ios - RestKit 连接失败委托(delegate)

标签 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.

问题是我想在委托(delegate)回调方法中处理这种情况,但没有调用任何委托(delegate)方法。我已根据请求设置委托(delegate),并已实现 requestDidFailLoadWithError、requestDidCancelLoad、requestDidTimeout 和 objectLoaderDidFailWithError。这些都没有被调用。

为什么我的委托(delegate)没有被召集?

编辑:在 RKRequest.m 中设置断点后,我看到实际上正在执行以下行:

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

但是,我的委托(delegate)方法没有被调用。

这是我设置委托(delegate)的地方:

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

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

将 performSelector 更改为常规方法调用出现在表面上,以提供我正在寻找的行为。这会破坏什么吗?我想我不确定为什么 performSelector 被用来调用同一个类中的方法。

编辑 3: 根据要求,这是我的委托(delegate)方法:

-(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];
}

最佳答案

编辑:

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)并检查会发生什么。如果您查看方法定义,对委托(delegate)的调用实际上就在那里:

    } 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/

相关文章:

ios - 检测 Sprite 纹理内的触摸而不是整个框架 iOS Swift SpriteKit?

ios - 将 tabBar iphone 应用程序移植到 iPad

ios - iPad 照片库框架

c# - 测试 WCF Web 服务

web-services - SEI 和 WSDL 有什么区别

ios - 将委托(delegate)从第三个设置为第一个 viewController

c# - 使用 Moq 验证采用委托(delegate)参数的基类中的方法调用

objective-c - UIButton 响应其之上的 UIPanGestureRecognizer

c# - 如何识别匿名函数

web-services - 在具有依赖关系的 Scala 代码中调用外部服务