14. GCD
• Less memory penalty
• Less configure
• Less manage
• Simplifies the code
15. GCD
• GCD的核⼼心是dispatch queues
• 它管理pools of threads
• 管理queues,⽽而⾮非直接管理threads
It was first released with Mac OS X 10.6, and is also
available with iOS 4 and above.
16. Thread pools
• Thread pools.
• Grab a thread from the pool and dispatch
the task to it
• Queue the task and wait for a thread to
become available.
17. Blocks
void (^aBlock)(int) = ^(int z) {!
!
NSLog(@“log”);!
!
};!
!
• Like a function pointer, except it also stores
the context the block was created in.
• Beware of retain cycles
22. Lockless Exclusion
• Serial queue are thread safe inside
-(void)accessSharedVariableAsync:(void(^)(id sharedVariable))block{
// myQueue must be a serial queue
dispatch_sync(myQueue, ^{
block([self sharedVariable]);
});
}
28. Best Sources
• How To Use NSOperations and
NSOperationQueues http://
www.raywenderlich.com/19788/how-touse-nsoperations-and-nsoperationqueues
29. • NSOperation vs Grand Central Dispatch
• http://stackoverflow.com/questions/10373331/
nsoperation-vs-grand-central-dispatch
• Grand Central Dispatch Design Patterns by
Robert Brown
• http://www.slideshare.net/robby_brown/grandcentral-dispatch-design-patterns