2013-12-12 2 views
1

클라우드에서 일부 데이터를로드하는 데 사용하는 NSURLSession이 있습니다. 나는NSURLSession, 데이터로드를 다시 시도하십시오.

[[session dataTaskWithURL:url completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { 
if (!error) { 
    NSHTTPURLResponse *httpResp = (NSHTTPURLResponse*) response; 
    if (httpResp.statusCode == 200) { 
     NSError *jsonError; 
     myArray = [[NSArray alloc] init]; 
     myArray = [NSJSONSerialization JSONObjectWithData:data 
                 options:NSJSONReadingAllowFragments 
                  error:&jsonError]; 
    } 
    } 
}] resume]; 

지금 몇 번 내가이 통과하고 myArray의가 하늘이 호출을 사용, 그래서 myArray의 데이터를로드 빈 재시 경우 내가 그 안에 체크를 넣을 수 있는지 여부를 알고 싶어!

답변

1

if 문을 추가하여 배열 개수가 0인지 확인한 다음 연결을 다시 시도하십시오.

[[session dataTaskWithURL:url completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { 
if (!error) { 
    NSHTTPURLResponse *httpResp = (NSHTTPURLResponse*) response; 
    if (httpResp.statusCode == 200) { 
     NSError *jsonError; 
     myArray = [[NSArray alloc] init]; 
     myArray = [NSJSONSerialization JSONObjectWithData:data 
                options:NSJSONReadingAllowFragments 
                 error:&jsonError]; 
     //Check for an empty array 
     if ([myArray count] == 0) { 
      //retry 
     } 
    } 
    } 
}] resume]; 
+0

어떻게 연결을 다시 시도합니까? – Eddie

+0

모든 것을 메소드에 넣고'[retry'을'[self nameOfMethod];로 대체하십시오. – werm098

관련 문제