2013-01-14 36 views
2

我试图在连接失败时测试我的应用程序的行为。我正在iPad上关闭WiFi测试。当Restkit试图调用Web服务,我得到以下错误:RestKit连接失败委托

CPL[7713:6203] E restkit.network:RKRequest.m:545 Failed to send request to https://xxxxxxxx/APNS_WebService/rest/operations/initializeDevice?deviceID=c4a17f855d3cc824b174b71908480d4e505ebfb221cb4643da9270a07344c367 due to unreachable network. 

的问题是,我想在处理委托的回调方法这种情况,但没有委托方法被调用。我已经在请求上设置了委托,并且实现了requestDidFailLoadWithError,requestDidCancelLoad,requestDidTimeout和objectLoaderDidFailWithError。这些都不是所谓的。

为什么不是我的代表被调用?

编辑:设置里面RKRequest.m断点后,我看到下面的行实际上是在被执行:

 [self performSelector:@selector(didFailLoadWithError:) withObject:error afterDelay:0]; 

然而,我的委托方法没有得到所谓的。

此处,我设置委托:

request = [client requestWithResourcePath:[NSString stringWithFormat:@"/initializeDevice?deviceID=%@",deviceID]]; 
request.delegate=self; 
[request sendAsynchronously]; 

编辑2:其实,在RKRequest.m行,我张贴以上只是调用RKRequest另一种方法,但事实并非如此。在didFailLoadWithError中放置一个断点表明该代码永远不会到达。我不明白为什么这不起作用。

将performSelector更改为常规方法调用会出现在表面上,以使我找到我正在查找的行为。这会打破什么?我想我不知道为什么performSelector被用来调用同一个类中的方法。

编辑3:按照要求,这里是我的委托方法:

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

    [request reset]; 
    [request send]; 
} 

回答

1

编辑:

Actually, the line in RKRequest.m that I posted above is just calling another method in RKRequest, except that it's not. Putting a breakpoint in didFailLoadWithError shows that this code is never reached. I don't get why that's not working.

这是很奇怪的。我会尽量全面清理项目并重建。

至于什么需要直接调用,而不是使用performSelector,你可以看到afterDelay

[self performSelector:@selector(didFailLoadWithError:) withObject:error afterDelay:0]; 

这将使didFailLoadWithError:方法在运行循环的下一次迭代被调用。我会保持这种称呼它的方式。

你可以尝试,不过,这个替代:

dispatch_async(dispatch_get_current_queue(), ^() { 
         [self didFailLoadWithError:error]; }); 

我建议设置你使用的是RestKit方法中的断点(我想sendAsynchronously),并检查发生了什么。如果您查看方法定义,则对代表的呼叫实际上存在于:

} else { 
     self.loading = YES; 

     RKLogError(@"Failed to send request to %@ due to unreachable network. Reachability observer = %@", [[self URL] absoluteString], self.reachabilityObserver); 
     NSString* errorMessage = [NSString stringWithFormat:@"The client is unable to contact the resource at %@", [[self URL] absoluteString]]; 
     NSDictionary *userInfo = [NSDictionary dictionaryWithObjectsAndKeys: 
            errorMessage, NSLocalizedDescriptionKey, 
            nil]; 
     NSError* error = [NSError errorWithDomain:RKErrorDomain code:RKRequestBaseURLOfflineError userInfo:userInfo]; 
     [self performSelector:@selector(didFailLoadWithError:) withObject:error afterDelay:0]; 
    } 
+0

谢谢,我会研究一下。 –

+0

请参阅我的编辑。 –

+0

你为什么不介入'didFailLoadWithError:'看看为什么不调用委托方法?它可能取决于您选择的缓存策略... – sergio