* * Dispatch_after Delay operation Application Scenario For example: the game background needs to do some random events, need to call the method after some time! 1> calls the method is usually related to the UI, such as prompting the user 2> do not understand GCD or multi-threaded people, you can directly fill in the blanks */-(void) Touchesbegan: (Nsset *) touches withevent: (Uievent * ) event{[self delay1];} #pragma mark-delay operation/** calls Dispatch_after */-(void) delay1{//1 in other threads. Queue dispatch_queue_t q = dispatch_queue_create ("Myqueue", dispatch_queue_concurrent); 2. Asynchronous Task Dispatch_async (q, ^{NSLog (@ "delay before start ....%@", [Nsthread CurrentThread]); Enter Dispatch_after//from the current time, after a delay of 2 seconds, add a task to the Home column (this task will "run asynchronously" on the main thread) Dispatch_after (Dispatch_time (dispatch_time _now, (int64_t) (2.0 * nsec_per_sec)), Dispatch_get_main_queue (), ^{NSLog (@ "%@", [Nsthread CurrentThread]); }); NSLog (@ "delay setting ends ...."); });} /** in the main thread call Dispatch_after */-(void) delay{NSLog (@ "delay before the start ...."); Enter Dispatch_after//from the current time, after a delay of 2 seconds, add a task to the Home column (this task will "run asynchronously" on the main thread) Dispatch_after (Dispatch_time (Dispatch_time_now, ( int64_t) (2.0 * nsec_per_sec)), Dispatch_get_maiN_queue (), ^{NSLog (@ "%@", [Nsthread CurrentThread]); }); NSLog (@ "Delay set end ....");} @end
**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!1>the method that is called is usually UI-related, such as prompting the user, etc.2>do not know GCD or multi-threaded people, you can directly fill in the blanks*/- (void) Touchesbegan: (Nsset *) touches withevent: (Uievent *)Event{[self delay1];}#pragmaMark-Delay operation/** Call Dispatch_after in other threads*/- (void) delay1{//1. Queuesdispatch_queue_t q = dispatch_queue_create ("Myqueue", dispatch_queue_concurrent); //2. Asynchronous TasksDispatch_async (q, ^{NSLog (@"before the delay begins ....%@", [Nsthread CurrentThread]); //input Dispatch_after//adds a task to the home column from the current time, after 2 seconds of delay (this task runs asynchronously on the main thread)Dispatch_after (Dispatch_time (Dispatch_time_now, (int64_t) (2.0* nsec_per_sec)), Dispatch_get_main_queue (), ^{NSLog (@"%@", [Nsthread CurrentThread]); }); NSLog (@"delay setting end ...."); });}/** Call Dispatch_after on the main thread*/- (void) delay{NSLog (@"before the delay begins ...."); //input Dispatch_after//adds a task to the home column from the current time, after 2 seconds of delay (this task runs asynchronously on the main thread)Dispatch_after (Dispatch_time (Dispatch_time_now, (int64_t) (2.0* nsec_per_sec)), Dispatch_get_main_queue (), ^{NSLog (@"%@", [Nsthread CurrentThread]); }); NSLog (@"delay setting end ....");}@end
iOS multi-thread GCD