2017-01-12 56 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)。私はなぜ整数私はが1から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 
      }); 
することによって、これを達成するための一つの方法があります
関連する問題