Difference between revisions of "Short Notes on MAC OSX"
From PaskvilWiki
(Created page with "== Asynchronous Execution == === NSThread === === performSelectorInBackground:withObject: === === dispatch_async() ===") |
|||
| (One intermediate revision by one user not shown) | |||
| Line 1: | Line 1: | ||
== Asynchronous Execution == | == Asynchronous Execution == | ||
| + | |||
| + | Here are 3 methods of executing code asynchronously. | ||
| + | |||
| + | Using <tt>NSThread</tt> (and threading in general) is the usual way, recommendable for longer-running threads. | ||
| + | |||
| + | The other 2 methods are typically recommended for use "locally", that is limited to the life-time of the object that spawns the async code. | ||
| + | |||
=== NSThread === | === NSThread === | ||
| − | === performSelectorInBackground:withObject: === | + | |
| + | '''TODO''' | ||
| + | |||
| + | === performSelectorInBackground:withObject: === | ||
| + | |||
| + | The <tt>performSelectorInBackground</tt> method of <tt>NSObject</tt> spawns a new thread to execute your provided method (via selector). | ||
| + | |||
| + | This also means that you have to take care of your release's or create a release pool, just as with <tt>NSThread</tt>. | ||
| + | |||
| + | Typical usage: | ||
| + | |||
| + | <pre>- (void)mainMethod | ||
| + | { | ||
| + | // ... | ||
| + | [self performSelectorInBackground:@selector(asyncMethod:) withObject:someParam]; | ||
| + | // code here is executed (almost) immediately after the above call, | ||
| + | // as the asyncMethod's code is now being executed in separate thread | ||
| + | // ... | ||
| + | } | ||
| + | NSArray* foo = [[[NSArray alloc] initWithObjects:@"a", @"b", nil] autorelease]; | ||
| + | [bar performSelectorInBackground:@selector(baz:) withObject:foo]; | ||
| + | |||
| + | - (void)asyncMethod:(id)param | ||
| + | { | ||
| + | NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init]; | ||
| + | // your code that will be executed asynchronously | ||
| + | [pool release]; | ||
| + | }</pre> | ||
| + | |||
=== dispatch_async() === | === dispatch_async() === | ||
| + | |||
| + | '''TODO''' | ||
Latest revision as of 12:37, 11 December 2012
Contents
Asynchronous Execution
Here are 3 methods of executing code asynchronously.
Using NSThread (and threading in general) is the usual way, recommendable for longer-running threads.
The other 2 methods are typically recommended for use "locally", that is limited to the life-time of the object that spawns the async code.
NSThread
TODO
performSelectorInBackground:withObject:
The performSelectorInBackground method of NSObject spawns a new thread to execute your provided method (via selector).
This also means that you have to take care of your release's or create a release pool, just as with NSThread.
Typical usage:
- (void)mainMethod
{
// ...
[self performSelectorInBackground:@selector(asyncMethod:) withObject:someParam];
// code here is executed (almost) immediately after the above call,
// as the asyncMethod's code is now being executed in separate thread
// ...
}
NSArray* foo = [[[NSArray alloc] initWithObjects:@"a", @"b", nil] autorelease];
[bar performSelectorInBackground:@selector(baz:) withObject:foo];
- (void)asyncMethod:(id)param
{
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
// your code that will be executed asynchronously
[pool release];
}
dispatch_async()
TODO