Grand Central Dispatch (GCD) is a solution for multi-core programming developed by Apple.
The dispatch queue is divided into the following three types:
1) Run the main queue in the main thread, obtained through Dispatch_get_main_queue.
2) Parallel queue Global dispatch queue, obtained by Dispatch_get_global_queue, creates three different priority dispatch queues by the system. Parallel queues are executed in the same order as they join the queue.
3) Serial queue serial queues is typically used to synchronize access sequentially, creating any number of serial queues that are concurrent between each serial queue.
Serial queues are useful when you want tasks to execute in a particular order. A serial queue performs only one task at a time. We can use serial queues instead of locks to protect shared data. Unlike locks, a serial queue guarantees that the task executes in a predictable order.
Serial queues is created through dispatch_queue_create and can be used to increase or decrease the reference count using function Dispatch_retain and dispatch_release.
the use of GCD :
Background execution:
Dispatch_async (dispatch_get_global_queue (0, 0), ^{
Something
});
Main thread Execution:
Dispatch_async (Dispatch_get_main_queue (), ^{
Something
});
Disposable execution:
Static dispatch_once_t Oncetoken;
Dispatch_once (&oncetoken, ^{
Code to be executed once
});
Delay of 2 seconds execution:
Double delayinseconds = 2.0;
dispatch_time_t poptime = Dispatch_time (Dispatch_time_now, delayinseconds * nsec_per_sec);
Dispatch_after (Poptime, Dispatch_get_main_queue (), ^ (void) {
Code to is executed on the main queue after delay
});
Custom dispatch_queue_t
dispatch_queue_t urls_queue = dispatch_queue_create ("blog.devtang.com", NULL);
Dispatch_async (Urls_queue, ^{
Your code
});
Dispatch_release (Urls_queue);
Merge Summary Results
dispatch_group_t group = Dispatch_group_create ();
Dispatch_group_async (Group, Dispatch_get_global_queue (0,0), ^{
Parallel execution of Line Cheng
});
Dispatch_group_async (Group, Dispatch_get_global_queue (0,0), ^{
Thread Two for parallel execution
});
Dispatch_group_notify (Group, Dispatch_get_global_queue (0,0), ^{
Summary results
});
An example of applying GCD:
Dispatch_async (Dispatch_get_global_queue (dispatch_queue_priority_default, 0), ^{
Nsurl * url = [nsurl urlwithstring:@ "http://www.baidu.com"];
Nserror * ERROR;
NSString * data = [NSString stringwithcontentsofurl:url encoding:nsutf8stringencoding error:&error];
if (data! = nil) {
Dispatch_async (Dispatch_get_main_queue (), ^{
NSLog (@ "Call back, the data is:%@", data);
});
} else {
NSLog (@ "error when download:%@", error);
}
});
Another use of GCD is to allow programs to run longer in the background.
When you do not use GCD, when the app is left on the home button, the app only has a maximum of 5 seconds to do some saving or cleanup work. But after using GCD, the app has a maximum of 10 minutes to run in the background for a long time. This time can be used to clean up the local cache, send statistics and other work.
The sample code that lets the program run long in the background is as follows:
AppDelegate.h file
@property (Assign, nonatomic) Uibackgroundtaskidentifier backgroundupdatetask;
APPDELEGATE.M file
-(void) Applicationdidenterbackground: (uiapplication *) application
{
[Self beingbackgroundupdatetask];
Add the code that you need to run long
[Self endbackgroundupdatetask];
}
-(void) Beingbackgroundupdatetask
{
Self.backgroundupdatetask = [[UIApplication sharedapplication] beginbackgroundtaskwithexpirationhandler:^{
[Self endbackgroundupdatetask];
}];
}
-(void) Endbackgroundupdatetask
{
[[UIApplication sharedapplication] endBackgroundTask:self.backgroundUpdateTask];
Self.backgroundupdatetask = Uibackgroundtaskinvalid;
}
IOS Multi-thread GCD