开发者社区> 问答> 正文

怎么在一个线程结束后回到主线程?

我用一个for循环上传三张照片(用的是afnetworking框架)。
上传它就开了一个线程,上传成功后后台返回一个filename,把filename放进一个数组
我想等三张照片全都上传结束后,对获取到的三个filename进行操作
要怎么做?

NSMutableArray *fileNameArr = [[NSMutableArray alloc]init];
 for (int i= 0; i<[self.imgArr count]; i++) {
    NSData *data = [self.imgArr objectAtIndex:i];
    [manager POST:url parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData){
        [formData appendPartWithFileData:data name:@"pic" fileName:fileName mimeType:@"image/jpeg"];
    }success:^(AFHTTPRequestOperation *operation,id responseObject){
        NSLog(@"success:%@",responseObject);
        NSString *str = [[responseObject objectForKey:@"data"] objectForKey:@"fileName"];
        [fileNameArr addObject:str];
        NSString *ids = [[responseObject objectForKey: @"data"] objectForKey:@"ids"];          
    }
          failure:^(AFHTTPRequestOperation *operation,NSError *error){
              NSLog(@"%@",error);
          }];
}

展开
收起
a123456678 2016-07-28 10:14:46 2249 0
2 条回答
写回答
取消 提交回答
  • dispatch_async(dispatch_get_main_queue(), ^{ //todo something }); 或者[self performSelectorOnMainThread:@selector(xxx:) withObject:self waitUntilDone:NO];
    2019-07-17 20:01:04
    赞同 展开评论 打赏
  • 第一种是Notification,这个比较解耦。

    -(void)viewDidLoad
    {
        // add a notification observer
        [NSNotificationCenter defaultCenter] addObserver:selector:name:object:]
    }
    -(void)handle
    {
        if (fileNameArr.count == 3) {
            // do something
        }
    }
    NSMutableArray *fileNameArr = [[NSMutableArray alloc]init];
    for (int i= 0; i<[self.imgArr count]; i++) {
        NSData *data = [self.imgArr objectAtIndex:i];
        [manager POST:url 
           parameters:nil 
           constructingBodyWithBlock:
         ^(id<AFMultipartFormData> formData){
             [formData appendPartWithFileData:data 
                                         name:@"pic" 
                                     fileName:fileName 
                                     mimeType:@"image/jpeg"];
         } success:^(AFHTTPRequestOperation *operation,id responseObject) {
             NSLog(@"success:%@",responseObject);
             NSString *str = [[responseObject objectForKey:@"data"] objectForKey:@"fileName"];
             [fileNameArr addObject:str];
             NSString *ids = [[responseObject objectForKey: @"data"] objectForKey:@"ids"];          
             // add here
             [NSNotificationCenter defaultCenter] postNotificationName:object:]
       } failure:^(AFHTTPRequestOperation *operation,NSError *error){
             NSLog(@"%@",error);
          }];
       }
    第二种就是,GCD啦,但是代码看起来耦合比较高。
    
    NSMutableArray *fileNameArr = [[NSMutableArray alloc]init];
    for (int i= 0; i<[self.imgArr count]; i++) {
        NSData *data = [self.imgArr objectAtIndex:i];
        [manager POST:url 
           parameters:nil
           constructingBodyWithBlock:
        ^(id<AFMultipartFormData> formData){
            [formData appendPartWithFileData:data 
                                        name:@"pic" 
                                    fileName:fileName 
                                    mimeType:@"image/jpeg"];
        } success:^(AFHTTPRequestOperation *operation,id responseObject){
            NSLog(@"success:%@",responseObject);
            NSString *str = [[responseObject objectForKey:@"data"] objectForKey:@"fileName"];
            [fileNameArr addObject:str];
            NSString *ids = [[responseObject objectForKey: @"data"] objectForKey:@"ids"];          
            // here
            if (fileNameArr.count == 3) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    // handle 
                });
            }
       } failure:^(AFHTTPRequestOperation *operation,NSError *error){
              NSLog(@"%@",error);
          }];
       }
    2019-07-17 20:01:04
    赞同 展开评论 打赏
问答标签:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
多IO线程优化版 立即下载
低代码开发师(初级)实战教程 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载