2013-12-12 24 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語句來檢查數組數是否等於零,如果是,則重試連接。

[[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

把所有這些放在一個方法中,然後用'[self nameOfMethod];'替換'// retry';'' – werm098

相關問題