【问题标题】:RestKit connection failure delegateRestKit 连接失败委托
【发布时间】:2013-01-14 15:13:36
【问题描述】:

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

【问题讨论】:

    标签: ios web-services delegates restkit


    【解决方案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];
        }
    

    【讨论】:

    • 谢谢,我会调查一下。
    • 为什么不进入didFailLoadWithError: 看看为什么不调用委托方法?这可能取决于您选择的缓存策略...
    • 你能不能把你的委托方法的定义也贴出来request:didFailLoadWithError:
    • 看起来 didFailLoadWithError: 出于某种原因实际上没有被调用...我的意思是 performSelector 语句已执行,但未到达代码。
    • 另外,他们在那里使用 performSelector 而不是仅仅调用 didFailLoadWithError 是否有原因?
    猜你喜欢
    • 1970-01-01
    • 2011-09-10
    • 2012-04-04
    • 1970-01-01
    • 1970-01-01
    • 2014-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多