search

Home  >  Q&A  >  body text

ios - afnetworking3.0 如何实现多任务并行请求数据

如题,afnetworking更新到3.0后,如何实现多任务请求数据

PHP中文网PHP中文网2889 days ago297

reply all(1)I'll reply

  • 阿神

    阿神2017-04-18 09:16:58

    You can download it through Group, use dispath_group_enter() and dispath_group_leave() to put the thread into the group, use dispath_group_notify to monitor whether the task has been downloaded, do not use dispath_group_wait to monitor, because dispath_group_wait will block the thread.

    I wrote an example below:

    NSString *urlStr1 = @"url1";
    NSString *urlStr2 = @"url2";
    NSString *urlStr3 = @"url3";
    NSArray *urlStrings = @[urlStr1, urlStr2, urlStr3];
    
    dispath_group_t requestGroup = dispath_group_create();
    
    for(NSString urlString in urlStrings) {  
        dispatch_group_enter(requestGroup);
        AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
        [manager GET:urlString parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
            NSLog(@"Success");
            dispatch_group_leave(requestGroup);
         } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
         
            NSLog(@"Error: %@", error);
            dispatch_group_leave(requestGroup);
        }];
    }
    dispatch_group_notify(requestGroup, dispatch_get_main_queue(), ^{
     
            //doSomething
    });
    
    

    I did it with all my hands, I hope I can give you a good review^_^
    This method seems a bit troublesome, if any master has a better method, I hope to share it

    reply
    0
  • Cancelreply