Grand Central Dispatch (GCD) is a new solution developed by Apple for multi-core programming. It was first launched in Mac OS X 10.6 snow leopard and recently introduced to ios4.0. GCD is an efficient and powerful technology that replaces technologies such as nsthread. GCD can fully handle asynchronous programming issues such as data locking and resource leakage. GCD can accomplish many things, but this article only focuses on some basic knowledge required to implement multithreading in IOS applications. Before getting started, you must understand that what you want to provide to the GCD queue is Code It is used to schedule the operation on the queue created by the system or the user.
Declare a queue
A user-created queue is returned as follows:
Dispatch_queue_t myqueue = dispatch_queue_create ("com. iphonedevblog. Post", null );
The first parameter identifies the queue, and the second parameter defines the queue parameters (currently not supported, so null is passed in ).
Execute a queue
The following code is passed in asynchronously:
Dispatch_async (myqueue, ^ {[self dosomething] ;});
First, pass in the created queue, and then provide the code block that is run by the queue.
Declare and execute a queue
If you do not need to retain the reference of the queue to be run, you can use the following code to implement the previous functions:
Dispatch_async (dispatch_queue_create ("com. iphonedevblog. Post", null), ^ {[self dosomething];});
Pause a queue
To suspend a queue, you can call the following code. Pausing a queue stops all Code related to the queue.
Dispatch_suspend (myqueue );
Restore a queue
Do not forget to restore a paused queue. The pause and recovery operations are similar to the retain and release operations in memory management. Calling dispatch_suspend will increase the pause count, while dispatch_resume will decrease. The queue starts to run only when the pause count turns to zero. Dispatch_resume (myqueue );
Run code in the main thread from the queue
Some operations cannot be run in the asynchronous queue, so they must be run in the main thread (each application has one. Ui plotting and any call to nsicationicationcenter must be performed on the master thread length. An example of accessing the main thread and Running code in another queue is as follows:
Dispatch_sync (dispatch_get_main_queue (), ^ {[self dismissloginwindow];});
Note that dispatch_suspend (and dispatch_resume) does not work on the main thread.