RestKit连接失败委托

时间:2013-01-14 15:13:37

标签: ios web-services delegates restkit

我正在尝试在连接失败时测试我的应用的行为。我正在关闭wifi的iPad上进行测试。当Restkit尝试Web服务调用时,我收到以下错误:

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 个答案:

答案 0 :(得分:1)

编辑:

  

实际上,我在上面发布的RKRequest.m中的行只是调用RKRequest中的另一个方法,除了它不是。在didFailLoadWithError中放置断点表明永远不会到达此代码。我不明白为什么那不起作用。

这真的很奇怪。我会尝试完全清理项目并重建。

关于直接调用而不是使用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];
    }