iOS multi-thread GCD

Source: Internet
Author: User

* * 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.