NSThread
There are two ways to create a thread using the
NSThread
class:- Use the
detachNewThreadSelector:toTarget:withObject:
class method to spawn the new thread. - Create a new
NSThread
object and call itsstart
method. (Supported only in iOS and OS X v10.5 and later.)
Both techniques create a detached thread in your application. A detached thread means that the thread’s resources are automatically reclaimed by the system when the thread exits. It also means that your code does not have to join explicitly with the thread later.
Because the
detachNewThreadSelector:toTarget:withObject:
method is supported in all versions of OS X, it is often found in existing Cocoa applications that use threads. To detach a new thread, you simply provide the name of the method (specified as a selector) that you want to use as the thread’s entry point, the object that defines that method, and any data you want to pass to the thread at startup. The following example shows a basic invocation of this method that spawns a thread using a custom method of the current object.[NSThread detachNewThreadSelector:@selector(myThreadMainMethod:) toTarget:self withObject:nil]; |
Prior to OS X v10.5, you used the
NSThread
class primarily to spawn threads. Although you could get an NSThread
object and access some thread attributes, you could only do so from the thread itself after it was running. In OS X v10.5, support was added for creating NSThread
objects without immediately spawning the corresponding new thread. (This support is also available in iOS.) This support made it possible to get and set various thread attributes prior to starting the thread. It also made it possible to use that thread object to refer to the running thread later.
The simple way to initialize an
NSThread
object in OS X v10.5 and later is to use the initWithTarget:selector:object:
method. This method takes the exact same information as the detachNewThreadSelector:toTarget:withObject:
method and uses it to initialize a new NSThread
instance. It does not start the thread, however. To start the thread, you call the thread object’s start
method explicitly, as shown in the following example:NSThread* myThread = [[NSThread alloc] initWithTarget:self |
selector:@selector(myThreadMainMethod:) |
object:nil]; |
[myThread start]; // Actually create the thread |
If you have an
NSThread
object whose thread is currently running, one way you can send messages to that thread is to use theperformSelector:onThread:withObject:waitUntilDone:
method of almost any object in your application. Support for performing selectors on threads (other than the main thread) was introduced in OS X v10.5 and is a convenient way to communicate between threads. (This support is also available in iOS.) The messages you send using this technique are executed directly by the other thread as part of its normal run-loop processing. (Of course, this does mean that the target thread has to be running in its run loop; see “Run Loops.”) You may still need some form of synchronization when you communicate this way, but it is simpler than setting up communications ports between the threads.
For a list of other thread communication options, see “Setting the Detached State of a Thread.”
NOTE -> You cannot perform UI Changes in background thread, for this you have to jump on main thread,
JUMP TO MAIN THREAD
dispatch_async(dispatch_get_main_queue(), ^{
//Your Task
});
or
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// background code
});
or
[self performSelectorOnMainThread:@selector(doYourUIChanges:) withObject:nil waitUntilDone:YES];
Comments
Post a Comment