5. GCD - Getting Started
Basics of blocks in Objective-C
5
- Segment of code
- Passed around as parameters
- Stored in variables
- Called like C functions ()
^{
// Some code
};
6. What is GCD about?
“…technology that you use to manage the
execution of tasks in your app…”
- Apple Docs
6
7. - C API (mostly)
- Open source!
- Task = ^{}
GCD - Intro
7
18. GCD - Scheduling Tasks with Queues
18
QOS_CLASS_UTILITY
QOS_CLASS_USER_INITIATED
19. GCD - Scheduling Tasks with Queues
Task
- dispatch_block_t b = ^{};
Schedule a task in GCD
- dispatch_sync(queue, ^{}) // Blocking
- dispatch_async(queue, ^{}) // Non blocking
19
20. GCD - Scheduling Tasks with Queues
- Special 6th global queue - main queue
- dispatch_get_main_queue()
- Executes blocks on the main thread
20
21. GCD - Scheduling Tasks with Queues
Common Pattern
dispatch_async(dispatch_get_global_queue(QOS_CLASS_DEFAULT), ^{
// do some expensive work
dispatch_async(dispatch_get_main_queue(), ^{
// show user the work i did
});
});
21
27. Better Solution - Dispatch Groups
dispatch_group_t
- dispatch_group_create();
- Creates a new empty group
- dispatch_group_enter(group);
- Increments the number of tasks in group
- dispatch_group_leave(group);
- Decrements the number of tasks in group
GCD - Synchronizing Tasks
27
28. Potential solution - Dispatch Groups
dispatch_group_t group = dispatch_group_create();
// For each task:
// call dispatch_group_enter(group) on start
// call dispatch_group_leave on tasks completion
// Call wait to block until task count is 0
dispatch_group_wait(group, DISPATCH_TIME_FOREVER);
GCD - Synchronizing Tasks
28
29. Potential solution - Dispatch Groups
dispatch_group_t group = dispatch_group_create();
// For each task:
// call dispatch_group_enter(group) on start
// call dispatch_group_leave on tasks completion
dispatch_async(someConcurrentQueue, ^{
// Call wait to block until task count is 0
dispatch_group_wait(group, DISPATCH_TIME_FOREVER);
});
GCD - Synchronizing Tasks
29
30. GCD - Synchronizing Tasks
Optimal Solution
- dispatch_group_notify(group, queue, block);
- Read “When there are no tasks left associated with this group, notify by
enqueueing this block onto this queue”
30
33. Sync Task
- Easily fits into a block
- Blocks until the task is done
Async Task
- Task happens in another thread/process
- Starting this task returns immediately
GCD - Serialize Async Tasks
33
37. GCD - Caveats
Cleanup is hard
- if a dispatch_group_t is deallocated with a non-
zero task count, it will throw an exception
- dispatch_async
“…The queue is retained by the system until the block
has run to completion…” - Apple Docs
37
38. Tools in the Toolbelt
GCD
- Small synchronous tasks
- Simple asynchronous tasks
NSOperation and NSOperationQueue
- Complex asynchronous tasks
- Cancel support
38
39. What else can I do with GCD?
- Efficient reader/writer schemes
- dispatch_barrier
- Read and write data efficiently
- dispatch_io (disk) and dispatch_data (memory)
- Respond to low-level system objects
- dispatch_source
39