UI and threading

Discussion in 'iOS Programming' started by Svinja, Dec 22, 2010.

  1. Svinja macrumors newbie

    Joined:
    Oct 27, 2008
    #1
    Hi,

    I was just wondering what are the rules about creating the UI(UIView or some control) on thread other than main? And how about then using those in another thread. I am asking this because i noticed i can create and change UI elements in some thread and it works fine on device, but crashes on simulator. Any kind of references, documentation, books would be appreciated thx...
     
  2. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    You should only do UI stuff, whether that is creating UI* objects, updating properties in UI* objects or redrawing the screen in the main thread.
     
  3. Svinja thread starter macrumors newbie

    Joined:
    Oct 27, 2008
    #3
    Well i know that is some general rule, but i fixed my problem and now it works flawlessly. This is my scenario:

    1. Create view in seperate thread.
    2. Take a screenshot of that view(in same thread)
    3. Put screenshot(image of view) in coverflow
    4. Release view and never use it again

    This is all done in same thread and it works. So i think maybe it would be correct to say that you can create UI on some thread but cannot change it in another thread, that is at least what i can see from experimenting...it doesnt matter if that thread is main or some other...that makes sense to me
     
  4. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #4
    I suspect it currently works on the device as it's got a single core and you are basically getting away with it. If/when (next year) the iPhone goes dual core I'd not be surprised if it starts failing.

    Apple make it very clear that they consider UIKit to only work from the main thread. See the note at the bottom of the page.
     
  5. firewood macrumors 604

    Joined:
    Jul 29, 2003
    Location:
    Silicon Valley
    #5
    The UI code is not reentrant, and thus background UI calls will cause random crashes. You don't have complete control over when the OS will do something with the UI in the foreground, and thus be corrupted by your background thread UIKit calls.

    Only use Core Graphics drawing in background threads.
     
  6. Svinja thread starter macrumors newbie

    Joined:
    Oct 27, 2008
  7. North Bronson macrumors 6502

    Joined:
    Oct 31, 2007
    Location:
    San José
    #7
    I'm pretty sure I've had good luck initializing UIImages on background threads (and passing them back to the main thread), but that might be the only UIKit class that works for that.
     

Share This Page