获得异步任务完成后的通知

队列中的异步任务完成后,可以用dispatch_group_notify()来获得通知。

执行时,手动触发dispatch_group_enter()和dispatch_group_leave()函数。

用法如下:

 // 创建一个串行队列
    dispatch_queue_t queue = dispatch_queue_create("com.test.queue", DISPATCH_QUEUE_SERIAL);

    // 创建group
    dispatch_group_t group = dispatch_group_create();

    // 异步任务1
    dispatch_async(queue, ^{
        dispatch_group_enter(group);    // 任务开始的时候,需要手动调用这个函数
        NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
        AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];
        NSURL *URL = [NSURL URLWithString:@"http://example.com/download1.zip"];
        NSURLRequest *request = [NSURLRequest requestWithURL:URL];
        NSURLSessionDownloadTask *downloadTask = [manager downloadTaskWithRequest:request progress:nil destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) {
            NSURL *documentsDirectoryURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil];
            return [documentsDirectoryURL URLByAppendingPathComponent:[response suggestedFilename]];
        } completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) {
            NSLog(@"File downloaded to: %@", filePath);
            dispatch_group_leave(group); // 异步执行完成后,需要手动调用这个函数
        }];
        [downloadTask resume];
    });

    // 异步任务2
    dispatch_async(queue, ^{
        dispatch_group_enter(group);    // 任务开始的时候,需要手动调用这个函数
        NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
        AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];
        NSURL *URL = [NSURL URLWithString:@"http://example.com/download1.zip"];
        NSURLRequest *request = [NSURLRequest requestWithURL:URL];
        NSURLSessionDownloadTask *downloadTask = [manager downloadTaskWithRequest:request progress:nil destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) {
            NSURL *documentsDirectoryURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil];
            return [documentsDirectoryURL URLByAppendingPathComponent:[response suggestedFilename]];
        } completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) {
            NSLog(@"File downloaded to: %@", filePath);
            dispatch_group_leave(group);    // 异步执行完成后,需要手动调用这个函数
        }];
        [downloadTask resume];
    });

    // group通知
    dispatch_group_notify(group, dispatch_get_main_queue(), ^{
        NSLog(@"全部下载完成!");
    });
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • NSThread 第一种:通过NSThread的对象方法 NSThread *thread = [[NSThrea...
    攻城狮GG阅读 4,326评论 0 3
  • 在这两部分的系列中,第一个部分的将解释 GCD 是做什么的,并从许多基本的 GCD 函数中找出几个来展示。在第二部...
    透支未来阅读 2,854评论 0 1
  • GCD GCD简介 Grand Central Dispatch中枢调度器 纯C语言的,提供了非常强大的函数 优势...
    彼岸的黑色曼陀罗阅读 3,369评论 0 0
  • 30 minutes with my baby nephew 07/29/2017 I spent 30 minu...
    李绅Luis阅读 1,472评论 0 0
  • 北方的村庄住着一个南方姑娘,她喜欢穿着带花的裙子站在路旁。 嘉懿遇到琪的那年他们都十八岁,后来嘉懿一个人去了南方的...
    关雎长乐阅读 3,680评论 5 13

友情链接更多精彩内容