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

Svinja

macrumors newbie
Original poster
Oct 27, 2008
27
0
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...
 
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.
 
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
 
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.
 
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.
 
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.