Contenu connexe
Similaire à Threading in iOS / Cocoa Touch (20)
Threading in iOS / Cocoa Touch
- 2. About Me
Used to be a Linux kernel developer
Currently developing hardware accessories for iOS
© 2011
- 5. What this talk isn’t about
Hardware accessories
App Store overnight success
© 2011
- 6. What this talk isn’t about
Hardware accessories
App Store overnight success
POSIX threads
© 2011
- 8. Plan
1. Why we love and hate threads
2. Grand Central Dispatch
3. NSThread class
© 2011
- 30. Serial queues
Useful for serialization of tasks
Created manually
dispatch_queue_create(“com.yourname.queue”, NULL);
© 2011
- 33. Dispatch source API
C-level API
Can monitor for events (signals, data) and dispatch
handlers to appropriate queues
Low-level, useful for OS X but maybe not for iOS
© 2011
- 35. The real thing
Cost of threads:
Creation time
Memory footprint (512 kB)
Design overhead
© 2011
- 39. Thread init
newThread = [[NSThread alloc] initWithTarget:self
selector:@selector(newThreadEntryPoint:) object:nil];
© 2011
- 43. Get to work
Two options:
Execute a predefined workset
Monitor for events
Run loop
© 2011
- 45. Running the loop
do {
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode
beforeDate:[NSDate distantFuture]];
} while (!done);
© 2011
- 46. The whole package
- (void)runThread
{
newThread = [[NSThread alloc] initWithTarget:self selector:@selector(newThreadEntryPoint:)
object:nil];
[newThread start];
}
- (void)newThreadEntryPoint:(id)param
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
[catnipStream setDelegate:self];
[catnipStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[catnipStream open];
do {
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
} while (!done);
}
© 2011
- 48. Run loop maintenance
do {
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode
beforeDate:[NSDate distantFuture]];
} while (!done);
© 2011
- 49. Run loop maintenance
do {
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode
beforeDate:[NSDate distantFuture]];
} while ([[NSThread currentThread] isCancelled] == NO);
© 2011
- 50. Run loop maintenance
do {
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode
beforeDate:[NSDate dateWithTimeIntervalSinceNow:5]];
} while ([[NSThread currentThread] isCancelled] == NO);
© 2011
- 52. A lot of code?
- (void)runThread
{
newThread = [[NSThread alloc] initWithTarget:self selector:@selector(newThreadEntryPoint:)
object:nil];
[newThread start];
}
- (void)newThreadEntryPoint:(id)param
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
[catnipStream setDelegate:self];
[catnipStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[catnipStream open];
do {
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate
dateWithTimeIntervalSinceNow:5]];
} while ([[NSThread currentThread] isCancelled] == NO);
[catnipStream close];
[catnipStream removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[catnipStream setDelegate:nil];
[pool drain];
}
© 2011
- 53. More maintenance
Consider draining the autorelease pool from within
the loop if the thread will be running for a long time
do {
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate
dateWithTimeIntervalSinceNow:5]];
[pool drain];
pool = [[NSAutoreleasePool alloc] init];
} while ([[NSThread currentThread] isCancelled] == NO);
© 2011
- 56. Caveats
Key-value observers are always executed in the
current thread context
What happens when KVO from secondary threads is
used to handle or update UI outlets?
© 2011
- 60. Questions?
Karol Kozimor
karol.kozimor@iosyn.com
© 2011
Notes de l'éditeur
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n