Monday 17 November 2008

Calling Nonatomic Properties in the Context of the Main Thread

In multithreaded applications, it may sometimes be helpful to call nonatomic properties in the context of the main thread. This is especially relevant if you want to update views (which run in the main thread) from other threads, i.e. a server accepting commands that change the properties of some view of the app.

Luckily, NSObject implements just such a function, which is called performSelectorOnMainThread, that lets you call the selector of an object in the main thread's context:


[myView performSelectorOnMainThread: @selector(update:) withObject: updateItems waitUntilDone: YES];


It even has the option to let the calling thread wait until the call has been completed!

No comments: