Making synchronous calls with RestKit(使用 RestKit 进行同步调用)
问题描述
我的实体用户有一个 objectMapping
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.
但是,我没能用 RestKit 做到这一点.我这边是不是有什么误会?我想要:
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];
推荐答案
如果你需要同步发送,你将无法使用 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:
RKObjectLoader* loader = [[RKObjectManager sharedManager] objectLoaderForObject:currentUser method:RKRequestMethodPUT delegate:nil];
RKResponse* response = [loader sendSynchronously];
这篇关于使用 RestKit 进行同步调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用 RestKit 进行同步调用
基础教程推荐
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01