Block of IOS GCD

Source: Internet
Author: User
Tags gcd instance method

    1. GCD deferred execution

#pragma mark-gcd delay/** * dispatch_after */-(void) gcddelay{NSLog (@ "Start time ....)    %@ ", [NSDate Date]);    Double delaysecond = 2.0;    dispatch_time_t delayinnanoseconds = Dispatch_time (Dispatch_time_now, delaysecond*nsec_per_sec);    Get the current global queue dispatch_queue_t Concurrentqueue = dispatch_get_global_queue (dispatch_queue_priority_default, 0);    Dispatch_after (Delayinnanoseconds, concurrentqueue, ^ (void) {NSLog (@ "Delay two seconds execution ...%@", [NSDate Date]); });}

The C function performs the following

/** * Dispatch_after_f performs a pure C function (processsomething) */-(void) gcddelay_t{NSLog (@ "Start time ....)    %@ ", [NSDate Date]);    Double delaysecond = 2.0;    dispatch_time_t delayinnanoseconds = Dispatch_time (Dispatch_time_now, delaysecond*nsec_per_sec);    Get the current global queue dispatch_queue_t Concurrentqueue = dispatch_get_global_queue (dispatch_queue_priority_default, 0); Dispatch_after_f executes a pure C function (processsomething) dispatch_after_f (delayinnanoseconds, Concurrentqueue, @ "GCD", processsomething);} void processsomething (void *paramcontext) {NSLog (@ "%@", Paramcontext);}

2.GCD single Case

-  (void) gcdsingle{    static dispatch_once_t oncetocken;         void  (^executedonlyonce) (void)  = ^{         static NSUInteger numberOfEntries = 0;         numberofentries++;        nslog (@ "Executed %lu  time (s) ",  (Unsigned long) numberofentries);    };         dispatch_queue_t concurrentqueue = dispatch_get_global_queue (DISPATCH_ queue_priority_default, 0);         dispatch_once (&onceTocken,  ^ (void) {        dispatch_async (concurrentqueue,  executedonlyonce);     });         dispatch_once ( &oncetocken, ^ (void) {&NBSp;       dispatch_async (concurrentqueue, executedonlyonce);     });     }

It can be found that the output is only one line, although we have called 2 times, because we passed the dispatch_once_t is the same, the compiler executes only once.

3.GCD Queue scheduling

/** *   problem: I use the method Dispatch_group_async for each task, is asynchronous AH. Why does the order not change?  *   answer: Because they are all assigned to the same queue Dispatch_get_main_queue ()   in! In a queue is serial ah. So, it's in order.  */-  (void) gcdgroup{    //  Create a dispatch group     dispatch_group_t  taskgroup = dispatch_group_create ();    //  Create a queue      dispatch_queue_t mainqueue = dispatch_get_main_queue ();         //  Task 1    //  adds a block to the specified dispatch group (Taskgroup), and the block uses the specified queue ( Mainqueue) execution.     dispatch_group_async (taskgroup, mainqueue, ^{         [self reloadtableview];    });         //  Task 2    //  adds a block to the specified dispatch group (Taskgroup), and the block is executed with the specified queue (mainqueue).     dispatch_group_async (Taskgroup, mainqueue, ^{        [self reloadscrollview];     });        //  Task 3    //  The block is added to the specified dispatch group (Taskgroup), and the block is executed with the specified queue (mainqueue).     dispatch_group_async (taskgroup, mainqueue, ^{         [self reloadimageview];    });         dispatch_group_notify (taskgroup, mainqueue, ^{         [[[uialertview alloc] initwithtitle:@ "complete"  message:@ "all dispatch complete"  delegate:nil  cancelbuttontitle:@ "complete"  OTHERBUTTONTITLES:NIL, NIL] SHOW];    });     }/** *   because Dispatch_group_async_f does not use block like dispatch_group_t, you can access the variables of the current class.   *  because  dispatch_group_async_f  accepts  C  functions as a block of code to execute, so we're going to execute ReloThe Adtableview method, Reloadscrollview method, Reloadimageview method, must have a reference to the current class!!!!!  *  then the  C  function must have a   reference to  self, which  Self  be able to invoke an instance method of the current object  * *//**  *  c function  */-  (void) gcdgroup_c{    //  Create a dispatch group      dispatch_group_t taskgroup = dispatch_group_create ();    //  Create queue     dispatch_queue_t mainqueue = dispatch_get_main_queue ();     dispatch_group_async_f (taskgroup, mainqueue,  (__bridge void *) self,  reloadallcomponents);     }void reloadallcomponents (Void *context) {     ViewController *self =  (__bridge viewcontroller *) context;     [self reloadTableView];    [self reloadScrollView];     [self reloadimageview];}

4. Custom Queue scheduling

-  (void) gcdcustomegroup{    dispatch_queue_t firstserialqueue =  Dispatch_queue_create ("COM.SINOICITY.GCD",  null);         dispatch _async (firstserialqueue, ^{        nsuinteger counter =  0;        for  (counter = 0; counter  < 5; counter++) {            nslog (@ " First iteration, counter = %lu ",  (unsigned long) counter);         }        nslog (@ "Current thread  = %@ ",  [nsthread currentthread]);     });         dispatch_async (firstserialqueue, ^{         nsuinteger counter = 0;        for  (counter = 0; counter <  5; counter++) {            nslog (@ " Second iteration, counter = %lu ",  (unsigned long) counter);         }        nslog (@ "Current thread  = %@ ",  [nsthread currentthread]);     });     Dispatch_async (firstserialqueue, ^{        nsuinteger  counter = 0;        for  (counter = 0;  counter < 5; counter++) {             nslog (@ "Third iteration, counter = %lu",  (unsigned long) counter);         }        nslog (@ "current thread = %@",  [NSThread  currentthread]);     });    dispatch_queue_t mainqueue  = dispatch_get_main_queue ();     dispatch_async (mainqueue, ^{         nslog (@ "main thread = %@",  [nsthread mainthread]);     });}

Note: Our custom queues are often not executed in the main queue. Instead, a single thread is allocated to maintain the queues we define.


This article is from the "one-mind" blog, please be sure to keep this source http://5007671.blog.51cto.com/4997671/1590715

Block of IOS 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.