使用 RestKit 进行同步调用
我的实体 User 有一个 objectMapping
RKObjectMapping* userMapping = [RKObjectMapping mappingForClass:[User class]];
[userMapping mapKeyPath:@"first_name" toAttribute:@"firstName"];
[userMapping mapKeyPath:@"last_name" toAttribute:@"lastName"];
[manager.mappingProvider setMapping:userMapping forKeyPath:@"users"];
[manager.mappingProvider setSerializationMapping:[userMapping inverseMapping] forClass:[User class]];
我想在应用程序进入后台时保存他的个人资料:
- (void)applicationDidEnterBackground:(UIApplication *)application
{
[[RKObjectManager sharedManager] putObject:currentUser delegate:self];
}
但它不会让我在关闭应用程序时运行请求(我猜测不允许新线程)。所以我想同步执行此操作。
然而,我没能用 RestKit 做到这一点。我这边是不是有什么误会?我想要:
[[RKObjectManager sharedManager] putObjectSynchronously:currentUser];
I have an objectMapping for my entity User
RKObjectMapping* userMapping = [RKObjectMapping mappingForClass:[User class]];
[userMapping mapKeyPath:@"first_name" toAttribute:@"firstName"];
[userMapping mapKeyPath:@"last_name" toAttribute:@"lastName"];
[manager.mappingProvider setMapping:userMapping forKeyPath:@"users"];
[manager.mappingProvider setSerializationMapping:[userMapping inverseMapping] forClass:[User class]];
And I want to save his profile when the application goes background:
- (void)applicationDidEnterBackground:(UIApplication *)application
{
[[RKObjectManager sharedManager] putObject:currentUser delegate:self];
}
But it won't let me run the request while closing down the app (I'm guessing no new threads are allowed). So I would like to do this synchronously.
However, I didn't manage to do this with RestKit. Is there some misunderstanding on my side ? I would like to have:
[[RKObjectManager sharedManager] putObjectSynchronously:currentUser];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您需要同步发送,您将无法使用 RKObjectManager 上的便捷方法(例如 putObject),因为这些便捷方法都代表您异步发送请求。相反,您可以尝试如下操作:
If you need to send synchronously, you're not going to be able to use the convenience methods on RKObjectManager, like putObject, because these convenience methods all send the request asynchronously on your behalf. Instead, you can try something like the following:
可以使用 RestKit 和 RKObjectManager 上的便捷方法来完成。技巧是使用块。
在您的情况下,您将使用块发出请求,在“加载器”上设置正常的 onDidFail、onDidLoadObjects、onDidLoadResponse 等方法,而不是作为类中的委托方法,然后关闭块内的应用程序。例如,loader.onDidLoadObjects 方法中的代码在 putObject 完成之前不会执行,因此您可能希望在此处关闭屏幕或应用程序。这将确保 putObject 在继续之前完成。
请参阅我的答案 - 使用 RestKit 进行同步 HTTP 调用。
It can be done using RestKit and the convenience methods on RKObjectManager. The tricks is to use blocks.
In your case you would make your request using the block, set the normal onDidFail, onDidLoadObjects, onDidLoadResponse, etc. methods on "loader" rather than as delegate methods in the class, and then close the app within the block. For example, the code within loader.onDidLoadObjects method will not be executed until the putObject has been completed, so that's likely where you would want to close the screen or app. This would ensure the putObject completes before moving on.
See more in my answer - Make a synchronous HTTP call with RestKit.
怎么样:
what about: