GCD使用案列

GCD官方文档

高级应用

笔记博客

关键方法:

dispatch_once

dispatch_async

//group

dispatch_group_async

dispatch_group_enter

dispatch_group_leave

dispatch_group_wait

dispatch_group_notify

//end group

 

dispatch_barrier_async

dispatch_apply

dispatch_time

dispatch_after

 

dispatch_semaphore_signal

1、单例

@implementation Person
+ (id)shareInstance
{
    static Person *per = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        per = [[Person alloc] init];
    });
    return  per;
}

2、Group

How to batch request with AFNetworking 2

// Create a dispatch group
dispatch_group_t group = dispatch_group_create();

for (int i = 0; i < 10; i++) {
    // Enter the group for each request we create
    dispatch_group_enter(group);

    // Fire the request
    [self GET:@"endpoint.json"
       parameters:nil
          success:^(NSURLSessionDataTask *task, id responseObject) {
                  // Leave the group as soon as the request succeeded
                  dispatch_group_leave(group);
          }
      failure:^(NSURLSessionDataTask *task, NSError *error) {
                  // Leave the group as soon as the request failed
                  dispatch_group_leave(group);
              }];
}

// Here we wait for all the requests to finish
dispatch_group_notify(group, dispatch_get_main_queue(), ^{
    // Do whatever you need to do when all requests are finished
});

发表评论

电子邮件地址不会被公开。 必填项已用*标注