使用 RestKit 进行同步调用 [英] Making synchronous calls with RestKit

查看:27
本文介绍了使用 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 进行同步调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆