### Preface: Not more new Jane book for a long time, hello, everyone 🤡. Today, I want to write some of my opinions on work or study. I have little knowledge and some parts are unavoidably omitted. I hope you can give me some advice in the following area.

In the first part, let’s talk about the group on GCD ##### we may have the following requirements: 1. Download multiple execution tasks, 2. Wait until all tasks are downloaded, then update the data or UI #####, we can do 👇

Dispatch_queue_t queue = dispatch_get_global_queue(0, 0); 2. Create a dispatch_group_t g = dispatch_group_create(); // 3. Add tasks to dispatch_group_async(group, queue, ^{NSLog(@)"A task: %@",[NSThread currentThread]);
    });
    
    dispatch_group_async(group, queue, ^{
        NSLog(@"B task: %@",[NSThread currentThread]);
    });
   
    dispatch_group_async(group, queue, ^{
        NSLog(@"C task: %@",[NSThread currentThread]); }); ^{dispatch_group_notify(group, dispatch_get_main_queue()) ^{ I'm not going to explain NSLog at sign"Update UI: %@", [NSThread currentThread]);
        
    });

Copy the code


Main_queue, ^{NSLog(@" there will be a deadlock here and this sentence will not be printed "); });Copy the code
It crashes as soon as it compiles. Why? I'll tell you the answer with a picture! ! PNG of synchronization task execution crash on main queue. (https://upload-images.jianshu.io/upload_images/2330091-8cb8b30640d29d67.png? ImageMogr2 /auto-orient/strip% 7CImageView2/2 /w/1240) the interesting thing is that adding a few lines of code here can make the application not crash, nothing is absolute, adding a synchronization task to the main queue does not necessarily crash!!Copy the code
Dispatch_queue_t queue = dispatch_queue_create("CR_QUEUE1", DISPATCH_QUEUE_CONCURRENT); Dispatch_async (queue, ^{NSLog(@" output currentThread, all clear 1: %@",[NSThread currentThread]); Dispatch_queue_t main_queue = dispatch_get_main_queue(); Main_queue, ^{NSLog(@" output currentThread, all clear 2: %@",[NSThread currentThread]); NSLog(@" No deadlocks here!" ); }); }); NSLog(@" I'll add this sentence here just to make sense of it ");Copy the code
Or a picture to explain:! The output explains everything. (https://upload-images.jianshu.io/upload_images/2330091-19655d2b63b8c0c0.png? imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)### Part 3, talk about the delay operation on GCD
#####        If one line of code doesn't make sense, I'll use two.

Copy the code
Dispatch_time_t when = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)); Dispatch_after (when, dispatch_queue_create("CC_GCD", NULL), ^{ NSLog(@"%@",[NSThread currentThread]); });Copy the code
After calling this method, a delay operation will be made. [Delay processing](https://upload-images.jianshu.io/upload_images/2330091-62f7f4ebb5b5c397.png? imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)### # Part 4, talk about one-time operations on GCD
#####        Dispatch_once is more used in singletons
Copy the code

for (int i = 0; i < 5; i++) { dispatch_async(dispatch_get_global_queue(0, 0), ^{

Create onceToken static dispatch_once_t onceToken; NSLog(@"onceToken: %ld", onceToken); // 2. Dispatch_once (&oncetoken, ^{NSLog(@"onceToken: %ld", onceToken); NSLog(@" I only output once %@", [NSThread currentThread]); }); });Copy the code

}

The code block inside dispatch_once is printed only once. However, the more familiar statement on the singleton is 👇Copy the code

//.m file static id _instance;

  • (instancetype)sharedInstance { static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ _instance = [[self alloc] init]; }); return _instance; }
  • (id)copyWithZone:(struct _NSZone *)zone { return _instance; }
  • (instancetype)allocWithZone:(struct _NSZone *)zone { static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ _instance = [super allocWithZone:zone]; }); return _instance; }
Madame SAO: In this article before 👉 [iOS developer "with 2 dollars to quickly create your website or blog"] (https://www.jianshu.com/p/dc4a4f1d5e1f) said to rapidly build server, with swift + perfect inside didn't understand some of the configuration, So I have temporarily can't write, settle down, we can win 💪 🏻 > finally say, most of domestic technology companies to develop iOS project basic use OC development, it is quite disappointed, hindered the motivation of our study swift, so in terms of the code of this article, the basic use OC to write, and forecast, the next month after the father's day, I will gradually update the iOS algorithm article, basically written with Swift, please look forward to 🌹 this article is not finished, to be continued......Copy the code