将 RestKit 作为 NSOpartion 执行
我目前正在将一个使用 ASIHTTPRequest 和 SBJson 的项目迁移到 RestKit。
之前的实现是使用 NSOperation 发出 HTTP 请求、解析 JSON 对象并对 Core Data API 进行必要的调用。
我已将其重构如下:
@implementation UpdateBeers
#pragma mark - NSOperation
- (void)main {
[[RKClient sharedClient] get:@"/beers" delegate:self];
}
- (void)request:(RKRequest*)request didLoadResponse:(RKResponse*)response {
debug(@"didLoadResponse");
}
- (void)request:(RKRequest *)request didFailLoadWithError:(NSError *)error {
debug(@"%@", error);
}
#pragma mark - Memory
- (void) dealloc {
[super dealloc];
}
@end
日志中显示以下内容
将 GET 请求发送到 URL http://localhost:9091 的 /api/测试。 HTTP Body:
问题是服务器从未收到请求。
将以下行 :
[[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.3]];
添加到 main 方法的末尾可以解决此问题。
我的问题是:
我是否应该将 ResKit API 调用作为 NSOperation 执行,如果不是,我可以选择在后台进行调用吗?
提前致谢。
I am currently migrating a project that used ASIHTTPRequest and SBJson to RestKit.
The previous implementation was using an NSOperation to make the HTTP Request, parse the JSON object and make the necessary calls to the Core Data API.
I have re-factored this as follows:
@implementation UpdateBeers
#pragma mark - NSOperation
- (void)main {
[[RKClient sharedClient] get:@"/beers" delegate:self];
}
- (void)request:(RKRequest*)request didLoadResponse:(RKResponse*)response {
debug(@"didLoadResponse");
}
- (void)request:(RKRequest *)request didFailLoadWithError:(NSError *)error {
debug(@"%@", error);
}
#pragma mark - Memory
- (void) dealloc {
[super dealloc];
}
@end
The following appears in the log
sending GET request to URL http://localhost:9091/api/test. HTTP Body:
The problem is the server never receives the request.
Adding the following line :
[[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.3]];
to the end of the main method solves this problem.
My question is :
Should I be executing ResKit API calls as an NSOperation and if not what are my alternatives for making calls in the background?
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
也许我没有完全理解这里的整体问题......RestKit 所做的关于从网络加载资源的所有操作都已经在后台进行了。几乎一切都已经是异步的,因此在 NSOperation 中运行现有的异步 RestKit 方法是重复的并且适得其反。话虽这么说,一旦 RestKit 告诉您它已完成下载数据 (didLoadResponse),您可能希望在 NSOperation 中进行任何后续后处理(如果该部分是计算密集型且与 UI 无关)。否则,不要试图超越 RestKit,只需像示例中那样运行它,您就可以享受异步的好处。
此外,如果您打算同时发出多个请求,您可能需要考虑使用 RestKit 的请求队列。它仍然会异步下载所有内容,但它只会一次运行您告诉它的请求数...当我的应用程序更新时,一次发送大约七个请求,但队列将串行运行它们而不是并行,从而防止任何网络带宽问题
Perhaps I'm not fully understanding the overall problem here ... Just about everything RestKit does with respect to loading resources from the network is already in the background. Almost everything is already asynchronous, and thus running the existing asynchronous RestKit methods inside an NSOperation is duplicative and counter productive. That being said, once RestKit tells you that it's finished downloading your data (didLoadResponse), you may want to do any subsequent post-processing in an NSOperation if that part is computationally intensive and unrelated to the UI. Otherwise, don't try to outthink RestKit, just run it as in the examples and you're good to go with asynchronous goodness.
Moreover, you may want to look at using RestKit's request queues if you plan to fire off several requests at the same time. It'll still download everything asynchronously, but it'll only run as many requests at a time as you tell it ... when my app is updating, send of about seven requests at once, but the queue will run them serially instead of parallel, thus preventing any network bandwidth issues