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

查看:118
本文介绍了使用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天全站免登陆