GCD multithreaded Operations
1) The most used operation
Get Global Queue
dispatch_queue_t queue = Dispatch_get_global_queue (Dispatch_queue_priority_default, 0);
Asynchronous execution
Dispatch_async (Queue, ^{
Download image
Dispatch_async (Dispatch_get_main_queue (), ^{
Back to the main thread update UI
});
});
?
2) Delay execution 1-call nsobject method [self performselector: @seletor (Run) Withobject:nil afterdelay:2.0];2-gcd (better, without regenerating method)
Dispatch_after (Dispatch_time (Dispatch_time_now, (int64_t) (2.0 * nsec_per_sec)), Dispatch_get_main_queue (), ^{
NSLog (@ "2222");
});
3) Guarantee method is executed only once throughout the program
Static dispatch_once_t Oncetoken;
Dispatch_once (&oncetoken, ^{
});
?
4) Group operations, multiple requests, request completion, and operation
Create a group,
dispatch_group_t group = Dispatch_group_create ();
Open a task download image 1
__block UIImage *image1 = nil;
Dispatch_group_async (Group, Dispatch_get_global_queue (Dispatch_queue_priority_default, 0), ^{
Image1 = [];
});
__block UIImage *image2 = nil;
Dispatch_group_async (Group, Dispatch_get_global_queue (Dispatch_queue_priority_default, 0), ^{
Image2 = [];
});
Dispatch_group_notify (Group, Dispatch_get_main_queue (), ^{
Image1
Image2
});
GCD Common operations