Creating UIWebView's in a different thread than the main one?

Discussion in 'iOS Programming' started by cstromme, Aug 23, 2009.

  1. cstromme macrumors regular

    Joined:
    Feb 26, 2007
    #1
    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?
     
  2. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #2
    Probably another reason to consider rethinking the UI, as I suggested in another thread.
     
  3. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #3
    You can't access any UI code from a background thread. You certainly can't create views on a background thread.
     
  4. cstromme thread starter macrumors regular

    Joined:
    Feb 26, 2007
    #4
    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.
     
  5. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #5
    Why does your table need to contain webViews? Can't each row just contain a kind of summary of each webView (text) and then have another view that actually shows the webView?
     
  6. stolidimran macrumors newbie

    Joined:
    Oct 1, 2009
    Location:
    Lahore, Pakistan
    #6
    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:
    A quick fix here could be to use "performSelectorOnMainThread" method instead of NSThread or NSOperation.

    I hope this is helpful!

     
  7. firewood macrumors 604

    Joined:
    Jul 29, 2003
    Location:
    Silicon Valley
    #7
    Why do you need to load the webview to calculate its height?

    Can you create the webviews and load their content later?
     

Share This Page