RestKit - 发布对象并更新其属性 [英] RestKit - Post object and update its attributes

查看:43
本文介绍了RestKit - 发布对象并更新其属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用 RestKit 和 Sinatra 支持的服务器的小应用程序.当我将用户对象发布到服务器时,服务器成功保存用户并使用新创建用户的 json 表示进行响应.

I have a small app using RestKit with a Sinatra-backed server. When I post a user object to the server, the server successfully saves the user and responds with a json representation of the newly created user.

这是在客户端上创建用户的代码:

Here's the code to create a user on the client:

  User *currentUser = [User currentUser];  
  currentUser.email = @"jacob@mail.com";
  currentUser.firstName = @"Jacob";
  currentUser.lastName = @"Morris";
  currentUser.phone = @"2088956709";
  currentUser.deviceId = @"MY-DEVICE-ID";
  currentUser.password = @"password";
  currentUser.passwordConfirmation = @"password";
  currentUser.timeStamp = [NSNumber numberWithFloat:3987987987.12233]; //totally random

  RKSpecResponseLoader *loader = [RKSpecResponseLoader responseLoader];
  [dataController.rkObjectManager postObject:currentUser mapResponseWith:[User rkObjectMapping] delegate:loader];
  [loader waitForResponse];
  NSLog(@"Current user id is now: %@", currentUser.userId);
  STAssertTrue([loader success], @"response from server should be a success");

从服务器返回的响应如下所示:

The response coming back from the server looks like this:

"{\"user\":{\"deviceId\":\"MY-DEVICE-ID\",\"email\":\"jacob@mail.com\",\"userId\":5,\"lastName\":\"Morris\",\"phone\":\"\",\"timeStamp\":3987987968.0}}"

服务器负责在成功创建对象后分配 userId.当响应回来时,我希望在客户端更新 currentUser 对象.我认为这应该是可能的,因为我将对已发布对象的引用传递给对象加载器.如何让对象加载器在成功响应后更新 userId?(我希望能够在不必捕获响应本身的情况下执行此操作.)

The server is responsible for assigning the userId upon successful creation of the object. When the response comes back, I'd like the currentUser object be updated on the client side. I'm thinking it should be possible since I'm passing a reference to the posted object to the object loader. How can I get the object loader to update the userId upon a successful response? (I'd like to be able to do this without having to capture the response itself.)

推荐答案

是什么让您认为 postObject 成功后新的 User 对象不会更新?

What makes you think the new User object isn't being updated once the postObject is successful?

我的理解是,如果当您 postObject 时,在 RKObjectLoader 中它会尝试将响应映射回作为默认行为 POST 的原始对象.让它不这样做的唯一方法是将目标对象开出账单.

My understanding if when you postObject, in the RKObjectLoader it will try to map the response back to the original object being POST'd as default behavior. The only way to get it to not do it is if you bill out the target object.

尝试这样的事情:

[[RKObjectManager sharedManager] postObject: currentUser delegate:self block:^(RKObjectLoader* loader) { 
            loader.resourcePath = @"/users";
            loader.objectMapping = [[RKObjectManager     sharedManager].mappingProvider objectMappingForKeyPath:@"/users"]; 
}];

只要您正确设置了映射(从服务器到 objc-model 和从 objc-model 到服务器)并且您的 KeyPath 设置正确(是 json { user : { user stuff} }或者只是 { user stuff } ) 那么它应该正常工作".

As long as you have the mappings setup correctly (coming from the server into the objc-model and the from objc-model to server) and you have your KeyPath setup correctly (is the json { user : { user stuff} } or just { user stuff } ) then it should "just work".

这篇关于RestKit - 发布对象并更新其属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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