连接失败时,我正在尝试测试应用程序的行为。我正在关闭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];
}

最佳答案

编辑:

实际上,我在上面发布的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];
    }

关于ios - RestKit连接失败委托(delegate),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14321119/

10-13 07:46
查看更多