2013-01-14 54 views
1

使用Restkit,我想重試一個失敗的請求。我試圖從委託方法做到這一點,如下所示:Restkit:重試失敗的請求

-(void)request:(RKRequest *)request didFailLoadWithError:(NSError *)error{ 
    NSLog(error.domain); 
    NSLog([NSString stringWithFormat:@"%d",error.code]); 
    NSLog(error.localizedDescription); 
    NSLog(error.localizedFailureReason); 
    [request cancel]; 
    [request reset]; 
    [request send]; 
} 

不過,我得到以下錯誤:

2013-01-14 11:19:29.423 Mobile_ACPL[7893:907] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Attempting to add the same request multiple times' 

我怎樣才能做到這一點?

回答

2

錯誤消息意味着您嘗試(重新)發送的request仍在某些內部隊列中。可能給系統更多的時間來處理cancelreset可能會使事情發揮作用。

嘗試這樣的:

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

    [request cancel]; 
    [request reset]; 

    dispatch_async(dispatch_get_current_queue(), ^() { 
     [request send]; 
    } 
} 

希望它能幫助。如果這不起作用,那麼可能延遲一些(重新)發送可能會有所幫助。這等於做(對於1.0秒延遲):

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_SEC * 1.0), 
       dispatch_get_current_queue(), ^() { 
     [request send]; 
    }); 

或者發出請求的copy共和發送這一點。

+0

工作。再次感謝! –

+0

注意'dispatch_get_current_queue()'從iOS 6.0開始已被棄用 – magma