One, delayed execution 1, iOS common delay execution there are 2 ways
(1) Method of calling NSObject
[Self performselector: @selector (Run) Withobject:nil Afterdelay:2.0]; // call the self's Run method after 2 seconds
(2) using the GCD function
Dispatch_after (Dispatch_time (Dispatch_time_now, (int64_t) (2.0 * nsec_per_sec)), Dispatch_get_main_queue () , ^{ /// 2 seconds after executing the code here asynchronously ... });
Note: The code that executes asynchronously here is placed in the main queue.
2, GCD delay execution
(1) Home row execution
1NSLog (@"before the delay begins ....");2 //input Dispatch_after3 //adds a task to the home column from the current time, after 2 seconds of delay (this task runs asynchronously on the main thread)4Dispatch_after (Dispatch_time (Dispatch_time_now, (int64_t) (2.0* nsec_per_sec)), Dispatch_get_main_queue (), ^{5NSLog (@"%@", [Nsthread CurrentThread]);6 });7NSLog (@"delay setting end ....");
Output Result:
before the delay begins ....
end of delay setting ....
<nsthread:0x8d53ea0>{name = (null), num = 1}
The result shows that the delay operation is run asynchronously in the main queue because there is a dispatch_get_main_queue parameter in the Dispatch_after
(2) asynchronous task, turn on sub-thread execution delay
1 //1. Queues2dispatch_queue_t q = dispatch_queue_create ("Myqueue", dispatch_queue_concurrent);3 4 //2. Asynchronous Tasks5Dispatch_async (q, ^{6NSLog (@"before the delay begins ....%@", [Nsthread CurrentThread]);7 //input Dispatch_after8 //adds a task to the home column from the current time, after 2 seconds of delay (this task runs asynchronously on the main thread)9Dispatch_after (Dispatch_time (Dispatch_time_now, (int64_t) (2.0* nsec_per_sec)), Dispatch_get_main_queue (), ^{TenNSLog (@"%@", [Nsthread CurrentThread]); One }); ANSLog (@"delay setting end ...."); -});
Output Result:
before the delay begins ..... <nsthread:0x8c3f870>{name = (null), num = 2}
end of delay setting ....
<nsthread:0x8c23a80>{name = (null), num = 1}
The result shows that the delay operation returns to the main thread execution
(3) The advantages of the deferred operation by default in the main queue execution:
The calling method is usually UI-related, such as prompting the user, and so on;
Do not know GCD or multi-threaded people, you can directly fill in the blanks.
(4) Dispatch_after delay Operation Application Scenario
For example: The game background needs to do some random events, need to call the method after a certain time.
After a while, taking a flower, risking something, accidentally doing something.
Second, the queue group (1) There are 1 kinds of requirements first: the asynchronous execution of 2 time-consuming operations second: After the completion of 2 asynchronous operations, and then back to the main thread to perform the operation if you want to quickly and efficiently achieve the above requirements, you can consider using a queue group
dispatch_group_t Group = 0), ^{ // Execute 1 time-consuming asynchronous operations 0 ), ^{ // Execute 1 time-consuming asynchronous operations ^{ // When the previous asynchronous operation is complete, return to the main thread ... });
(2) Examples of reality
Analog Download TXT file
Multithreading _06_GCD Other uses in iOS