Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

cstromme

macrumors regular
Original poster
Feb 26, 2007
162
0
So I have a situation here where I'm creating UIWebViews and adding them to an array to later be used in a tableview. I had to do it this way for 2 reasons, one to be able to calculate the height of each row correctly, and two to be able to scroll the tableview without having the webviews slowing it down because they were still loading the content. So now I just create all 15 in advance and then when they're done loading I reload the table.

Anyway, this has led to the app freezing while the webviews are being created and loading. I figured that I should just run this method in it's own thread with NSThread or NSOperations, but whenever I do that I get the message that:
bool _WebTryThreadLock(bool), 0x393aae0: Tried to obtain the web lock from a thread other than the main thread or the web thread. This may be a result of calling to UIKit from a secondary thread. Crashing now...

So. How would I handle this? Loading the webviews lazily isn't an option here, but having the app freeze while they're loading isn't very desirable either. Is there really no way to run this in another thread?
 
You can't access any UI code from a background thread. You certainly can't create views on a background thread.
 
Probably another reason to consider rethinking the UI, as I suggested in another thread.

Believe me, I've tried to think of another way of doing this.

The problem is that I need the views loaded for when I present them in the tableview, also the fact that I need them to calculate the height of the row which means I need to create them earlier anyway.
 
Hope this help!

I believe this is because you are using some UI related method asynchronously [in your NSThread/ NSOperation method].

As Apple states in its documentation:
Code associated with your user interface typically must execute only in your application’s main thread.

A quick fix here could be to use "performSelectorOnMainThread" method instead of NSThread or NSOperation.

I hope this is helpful!

So I have a situation here where I'm creating UIWebViews and adding them to an array to later be used in a tableview. I had to do it this way for 2 reasons, one to be able to calculate the height of each row correctly, and two to be able to scroll the tableview without having the webviews slowing it down because they were still loading the content. So now I just create all 15 in advance and then when they're done loading I reload the table.

Anyway, this has led to the app freezing while the webviews are being created and loading. I figured that I should just run this method in it's own thread with NSThread or NSOperations, but whenever I do that I get the message that:


So. How would I handle this? Loading the webviews lazily isn't an option here, but having the app freeze while they're loading isn't very desirable either. Is there really no way to run this in another thread?
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.