使用 RestKit 进行同步调用

发布于 2024-12-03 15:00:07 字数 835 浏览 2 评论 0原文

我的实体 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

狠疯拽 2024-12-10 15:00:07

如果您需要同步发送,您将无法使用 RKObjectManager 上的便捷方法(例如 putObject),因为这些便捷方法都代表您异步发送请求。相反,您可以尝试如下操作:

RKObjectLoader* loader = [[RKObjectManager sharedManager] objectLoaderForObject:currentUser method:RKRequestMethodPUT delegate:nil];
RKResponse* response = [loader sendSynchronously];

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];
潦草背影 2024-12-10 15:00:07

可以使用 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.

み青杉依旧 2024-12-10 15:00:07

怎么样:

RKRequest *request = [client post:@"/service" params:params delegate:restDelegate];

RKResponse *response = [request sendSynchronously];

what about:

RKRequest *request = [client post:@"/service" params:params delegate:restDelegate];

RKResponse *response = [request sendSynchronously];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文