如何在 RestKit 中提前取消请求并调用“didFailWithError”
我在 Objective-C 项目中使用 RestKit,并且需要为调用我的服务指定大约 10 秒的超时。
仔细阅读后,RestKit 似乎不支持此功能,因此我的计划是:
- 发送请求时启动计时器
- 加载数据时,禁用计时器
这是我的问题...< /em>
如果计时器方法触发,我需要取消请求并手动调用下面的方法。我不是 100% 确定如何实现这一目标。
我的其他问题中有一些上下文,展示了 RestKit 的作用在我的项目中实施以及它在本例中的作用。
非常感谢您在这方面能给我的任何帮助。
- (void)objectLoader:(RKObjectLoader*)objectLoader didFailWithError:(NSError*)error {
NSLog(@"Hit error: %@", error);
}
I am using RestKit in my Objective-C project and need to specify a timeout for a call to my service of around 10 seconds.
After reading around, it doesn't look like RestKit supports this, so my plan is to:
- Start a timer when my request is sent
- When the data is loaded, disable the timer
Here's my problem...
If the timer method fires, I need to cancel the request and invoke the method below manually. I'm not 100% sure how to achieve this.
There's some context in my other question, showing how RestKit is implemented in my project and what it's doing in this case.
Many thanks in advance for any help you can give me on this one.
- (void)objectLoader:(RKObjectLoader*)objectLoader didFailWithError:(NSError*)error {
NSLog(@"Hit error: %@", error);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在 RestKit 版本 0.20.x 中,您可以使用取消预定请求
In RestKit version 0.20.x you can cancel scheduled requests using
您可以使用 cancelRequestsWithDelegate: 选择器来实现所描述的工作流程。
但是,调用该委托错误处理程序可能很棘手,但您可以通过创建新的、单独的错误处理程序来解决它,如下所示:
并修改
didFailWithError:
方法的主体:You can use cancelRequestsWithDelegate: selector in order to achieve the described workflow.
However, it might be tricky to invoke that delegate error handler, but you can work around it by creating new, separate error handler like this:
and modify the body of your
didFailWithError:
method:你总是可以得到:
<代码>
[[RKObjectManager共享管理器].operationQueue cancelAllOperations];
您的每个请求都将以错误 -999 结束(操作已取消)。您可以检查错误代码并执行适当的操作。
You can always end up with:
[[RKObjectManager sharedManager].operationQueue cancelAllOperations];
Each of your requests will finish with error -999 (operation cancelled). You can check the error.code and perform appropriate action.