GCD
A. GCD Overview
two. General methods of use of GCD
three. GCD----asynchronously downloads the picture and refreshes the UI.
Four. Gcd+block
51 sexual Executions:
six-Delay execution
Seven the correspondence between dispatch_time_t and second
A. GCD Overview
Grand Grand Central Dispatch Dispatch
is multi-threaded, similar to Nsthread, but much more powerful than nsthread
The architecture core is the queue and execution methods, dispatch_queue_t and Dispatch_async (or Dispatch_sync, which are not generally used by synchronous methods)
Two. General methods of use of GCD
There are three ways to use GCD:
The first type: Create a serial sub-thread queue
Create a serial dispatch queue (this method is not recommended, usually to get instead of create)
The next one executes after the first execution, and if the first one takes longer, the next waits
First parameter: A unique identifier, a string of C
The second parameter: is the description of this queue, is (serial/parallel), can be directly written null
dispatch_queue_tQueue =dispatch_queue_create("COM.ZHUOYOUAPP.AGCD",NULL);
Synchronous execution, will be in a wait state, may be stuck to the main thread
Dispatch_sync(Queue, ^{
Synchronous execution is performed in the main thread
NSLog (@ "-----%d----", [Nsthread Ismainthread]);
});
Asynchronous execution, immediate return, no impact on the main thread
Dispatch_async (Queue, ^{
NSLog (@ "----2-----");
[Nsthread Sleepfortimeinterval:5];
NSLog (@ "----3-----");
});
Dispatch_async (Queue, ^{
[Nsthread Sleepfortimeinterval:5];
Asynchronous execution of a serial queue is performed in a sub-thread
NSLog (@ "----%d-----", [Nsthread Ismainthread]);
});
Dispatch_async (Queue, ^{
[Nsthread Sleepfortimeinterval:3];
NSLog (@ "----2-----");
});
Dispatch_async (Queue, ^{
[Nsthread sleepfortimeinterval:1];
NSLog (@ "----3-----");
});
second type: Get a parallel split-thread queue
First parameter: priority, select default
Second parameter: Mark, write 0
dispatch_queue_t queue2 = dispatch_get_global_queue (dispatch_queue_priority_default, 0);
Dispatch_async (Queue2, ^{
[Nsthread sleepfortimeinterval:1];
Done in sub-threading, if you have a lot of data to download, you can put it here
NSLog (@ "----%d-----", [Nsthread Ismainthread]);
});
Dispatch_async (Queue2, ^{
[Nsthread Sleepfortimeinterval:2];
NSLog (@ "----2-----");
});
Dispatch_async (Queue2, ^{
[Nsthread Sleepfortimeinterval:3];
NSLog (@ "----3-----");
});
Third type: Get the main thread queue
dispatch_queue_t queue3 = Dispatch_get_main_queue ();
Dispatch_async (Queue3, ^{
Executed in the main thread, if there is an action to refresh the UI, write here
NSLog (@ "----%d-----", [Nsthread Ismainthread]);
});
Dispatch_async (Queue3, ^{
NSLog (@ "----2-----");
});
Dispatch_async (Queue3, ^{
NSLog (@ "----3-----");
});
three. GCD----asynchronously downloads the picture and refreshes the UI.
/***************************************** Key Content *********************************************/
Download and Refresh the UI
Dispatch_async (Dispatch_get_global_queue (dispatch_queue_priority_default, 0), ^{
NSData *data = [NSData datawithcontentsofurl:[nsurl urlwithstring:@ "/http/Baidu's image address];
UIImage *image = [UIImage imagewithdata:data];
Dispatch_async (Dispatch_get_main_queue (), ^{
Self.imageView.image = image;
});
});
/****************************************************************************************************/
Four. Gcd+block
Using block in GCD will retain a local variable with no __block adornments, whether it is a basic data type or an object pointer or a formal parameter, and will be automatically release after execution.
The reference count is released after the line is finished
Zytest *test = [[Zytest alloc] init];
NSLog (@ "1--->%d", test.retaincount);
dispatch_queue_t queue4 = dispatch_get_global_queue (dispatch_queue_priority_default, 0);
Dispatch_async (Queue4, ^{
NSLog (@ "2--->%d", test.retaincount);
Test.date = [NSDate Date];
});
[Nsthread sleepfortimeinterval:1];
NSLog (@ "3--->%d", test.retaincount);
Block in GCD will retain the parameter object once, regardless of the type of the original parameter (__block, local, global)
t = [[Zytest alloc] init];
[Self test:t];
[t release];
Content of the test method:
-(void) Test: (Zytest *) test
{
NSLog (@ "1--->%d", test.retaincount);
dispatch_queue_t queue4 = dispatch_get_global_queue (dispatch_queue_priority_default, 0);
Dispatch_async (Queue4, ^{
NSLog (@ "2--->%d", test.retaincount);
Test.date = [NSDate Date];
});
[Nsthread sleepfortimeinterval:1];
NSLog (@ "3--->%d", test.retaincount);
}
five , One-time execution:
Disposable execution:
Static dispatch_once_t Oncetoken;
Dispatch_once (&oncetoken, ^{
Code to be executed once
});
Vi. Deferred Implementation
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
});
Seven the correspondence between dispatch_time_t and second
dispatch_time_t poptime = Dispatch_time (Dispatch_time_now, 3 * nsec_per_sec);
GCD Thread Handling