2017-01-12 4 views
0

나는 JSON 데이터의 수를 얻고, 그리고 모든 일을 다운로드 또는 JSON을 가져 오는 프로세스가 완료되면 내가있는 tableViewNSURLSessionDataTask가 완료되었는지 확인하는 방법은 무엇입니까?

for(int i = 1;i <= self.numberOfEpisodes;i++) 
{ 
    NSString *endPoint = [[[[urlJson stringByAppendingString:getEpisodes]stringByAppendingString:title]stringByAppendingString:@"&episodeNumber="]stringByAppendingString:[NSString stringWithFormat:@"%i",i]]; 
    endPoint = [endPoint stringByReplacingOccurrencesOfString:@" " withString:@"%20"]; 
    NSLog(@"End point %@",endPoint); 
    [downloadJson:endPoint WithHandler:^(__weak id result) 
    { 
     NSArray *episodeArray =result; 
     if(episodeArray && episodeArray.count > 0) 
     { 
      for(NSDictionary *dictionary in episodeArray) 
      { 
       //parse stuff here 
      } 
     } 
     if(i == self.numberOfEpisodes) 
     { 
      //update UI 
     } 

    }]; 
} 

문제의 UI를이 줄 에 업데이트해야 if (i == self.numberOfEpisodes). 내가 왜 정수가 self.numberOfEpisodes에 배열되어 있지 않은지 모르겠다. 대신 1에서 self.numberOfEpisodes까지의 난수입니다. 그래서 json을 얻는 것이 완료되었는지 확인하는 다른 방법이 있습니까?

(void)getJson:(NSString *)urlString WithHandler:(void(^)(__weak id result))handler 
{ 
NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration defaultSessionConfiguration]; 
NSURLSession *defaultSession = [NSURLSession sessionWithConfiguration: defaultConfigObject delegate: nil delegateQueue: [NSOperationQueue mainQueue]]; 
NSURL * url = [NSURL URLWithString:urlString]; 
NSMutableURLRequest * urlRequest = [NSMutableURLRequest requestWithURL:url]; 

[urlRequest setValue:@"application/json" forHTTPHeaderField:@"Content-type"]; 
[urlRequest setValue:@"application/json" forHTTPHeaderField:@"Accept"]; 
[urlRequest setHTTPMethod:@"GET"]; 

NSURLSessionDataTask * dataTask =[defaultSession dataTaskWithRequest:urlRequest 
                completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { 
                 if(error == nil) 
                 { 
                  id returnedObject = [NSJSONSerialization JSONObjectWithData: data options: NSJSONReadingMutableLeaves error:nil]; 
                  handler(returnedObject); 
                 } 
                 else{ 
                  NSLog(@"error %@",error); 
                 } 
                }]; 
[dataTask resume]; 
} 

답변

0

dispatch_group_t

//Creat group 
    dispatch_group_t group = dispatch_group_create(); 

    for(int i = 1;i <= self.numberOfEpisodes;i++) 
    { 

    //Enter group 
    dispatch_group_enter(group); 

     NSString *endPoint = [[[[urlJson stringByAppendingString:getEpisodes]stringByAppendingString:title]stringByAppendingString:@"&episodeNumber="]stringByAppendingString:[NSString stringWithFormat:@"%i",i]]; 
     endPoint = [endPoint stringByReplacingOccurrencesOfString:@" " withString:@"%20"]; 
     NSLog(@"End point %@",endPoint); 
     [downloadJson:endPoint WithHandler:^(__weak id result) 
     { 

      NSArray *episodeArray =result; 
      if(episodeArray && episodeArray.count > 0) 
      { 
       for(NSDictionary *dictionary in episodeArray) 
       { 
        //parse stuff here 
       } 
      } 

    // Then Leave group 
      dispatch_group_leave(group); 

     }]; 
    } 
//Here you will be notified after it's done 

    dispatch_group_notify(group, dispatch_get_main_queue(), ^{ 
       // Do whatever you need to do when all requests are finished 
       //Update UI 
      }); 
하여이를 달성하기위한 하나 개의 방법이
관련 문제