2016-11-28 51 views
0

我在Json上使用GET方法。 GET方法在for循環中,問題在於它沒有完成任務或沒有得到結果。而循環增量。我在塊中放置了一個斷點,我將結果數據設置爲一個NSDictionary,但它永遠不會去那裏。等待或不退出獲取json的代碼塊

是否可以直接調用GET方法。我的意思是代碼將逐行讀取。它不會跳過或等待json完成處理?

這裏是我做了什麼:

- (void)downloadJsonFeed 
{ 
for(int i = 1;i < self.numberOfEpisodes;i++) 
{ 
    NSString *endPoint = [[[[baseJsonUrl stringByAppendingString:getEpisodes]stringByAppendingString:self.title]stringByAppendingString:@"&episodeNumber="]stringByAppendingString:[NSString stringWithFormat:@"%i",i]]; 
    NSLog(@"End point %@",endPoint); 
    [JsonDownload getJson:token andEndpointString:endPoint WithHandler:^(__weak id result) 
    { 
     NSArray *episodeArray =result; 
     //will do some task here 
    }]; 

} 

} 


- (void)getJson:(NSString *)authData andEndpointString:(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]; 

//NSString *auth = [NSString stringWithFormat:@"Bearer {%@}", authData]; 

[urlRequest setValue:@"application/json" forHTTPHeaderField:@"Content-type"]; 
[urlRequest setValue:@"application/json" forHTTPHeaderField:@"Accept"]; 
[urlRequest setValue:authData forHTTPHeaderField:@"Cookie"]; 
[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]; 
} 

我已經放在一個破發點中的這條線NSArray *episodeArray =result;和它永遠不會去那裏。但是當我把斷點[JsonDownload getJson:token andEndpointString:endPoint WithHandler:^(__weak id result)行它是響應

而在評論行//will do some task here我需要一個任務之前再次獲得另一個JSON。但我不能讓它永遠不會進入代碼塊

回答

0

通過將%endpoint中的空白字符替換爲%20來解決問題