2012-08-28 112 views
2

我試圖讓我的應用程序重試RKRequests,如果它們失敗。我試圖做這種方式:RestKit:如何重試失敗的請求?

- (void)objectLoader:(RKObjectLoader *)objectLoader didFailWithError:(NSError *)errorIn 
{ 
    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate]; 
    [appDelegate.waitView stop]; 

    NSInteger code = objectLoader.response.statusCode; 

    if (201 != code && 200 != code) 
    { 
     // determine whether to requeue the request or not 

     if ([objectLoader.userData isEqualToString:@"capture"]) // requeue captures 
     { 
      NSLog(@"requeueing request"); 
      [objectLoader.queue cancelRequest:objectLoader]; 
      [objectLoader send]; 
     } 
    } 
} 

...但它總是崩潰,因爲objectLoader似乎是cancelRequest線後懸擺指針。如果RKRequest發生故障,我如何在不崩潰的情況下重試它?

回答

0

您可以嘗試訪問RKRequest,例如,使用RKRequestDelegate協議:

- (void)request:(RKRequest *)request didFailLoadWithError:(NSError *)error 

因爲那麼你可以做到以下幾點:

NSAssert(!request.isUnsent && !request.isLoading, @"Cant retry load, current attempt has not completed"); 
[request reset]; 
[request send]; 
+0

當我嘗試這一點,斷言總是熄滅,因爲request.isLoading仍設置爲true。如果我將斷言註釋掉,我會在[請求發送]中得到「無法多次添加相同的請求」錯誤。如果我做[請求sendAsynchronously],請求立即失敗。爲什麼要讓RestKit做一個簡單的重試是非常困難的? –

+0

您是否嘗試手動取消請求?然後斷言不應該失敗。 – Nicholas