iOS 要求执行完 任务A,B 后才能执行任务 C

NSBlockOperation can't handle asynchronous operations。

A,B,C是同步执行的任务

用 NSOpertion 和 NSOpertionQueue 处理

NSOperationQueue *queue = [[NSOperationQueue alloc] init]; // 创建3个操作 NSOperation *a = [NSBlockOperation blockOperationWithBlock:^{ NSLog(@”operationA---“);}]; NSOperation *b = [NSBlockOperation blockOperationWithBlock:^{ NSLog(@”operationB---“);}]; NSOperation *c = [NSBlockOperation blockOperationWithBlock:^{ NSLog(@”operationC---“);}]; // 添加依赖 [c addDependency:a]; [c addDependency:b]; // 执行操作 [queue addOperation:a]; [queue addOperation:b]; [queue addOperation:c];

A,B,C是异步执行的任务,eg.网络请求

Dispatch Group 会在整个组的任务都完成时通知你。这些任务可以是同
步的,也可以是异步的,即便在不同的队列也行。
`
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{ // 1

    __block NSError *error;
    dispatch_group_t downloadGroup = dispatch_group_create(); // 2

    for (NSInteger i = 0; i < 3; i++) {
        NSURL *url;
        switch (i) {
            case 0:
                url = [NSURL URLWithString:kOverlyAttachedGirlfriendURLString];
                break;
            case 1:
                url = [NSURL URLWithString:kSuccessKidURLString];
                break;
            case 2:
                url = [NSURL URLWithString:kLotsOfFacesURLString];
                break;
            default:
                break;
        }

        dispatch_group_enter(downloadGroup); // 3
        Photo *photo = [[Photo alloc] initwithURL:url
                              withCompletionBlock:^(UIImage *image, NSError *_error) {
                                  if (_error) {
                                      error = _error;
                                  }
                                  dispatch_group_leave(downloadGroup); // 4
                              }];

        [[PhotoManager sharedManager] addPhoto:photo];
    }
    dispatch_group_wait(downloadGroup, DISPATCH_TIME_FOREVER); // 5
    dispatch_async(dispatch_get_main_queue(), ^{ // 6
        if (completionBlock) { // 7
            completionBlock(error);
        }
    });
});

`

你可能感兴趣的:(iOS 要求执行完 任务A,B 后才能执行任务 C)