RestKit - 发布对象并更新其属性

发布于 2024-12-24 19:06:25 字数 1486 浏览 1 评论 0原文

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

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

  User *currentUser = [User currentUser];  
  currentUser.email = @"[email protected]";
  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");

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

"{\"user\":{\"deviceId\":\"MY-DEVICE-ID\", \"电子邮件\":\"[电子邮件受保护]\",\"userId\":5,\"lastName\":\"Morris\",\"phone\":\"\" ,\"timeStamp\":3987987968.0}}"

服务器负责成功创建对象后分配userId。 当响应返回时,我希望在客户端更新 currentUser 对象。我认为这应该是可能的,因为我将对已发布对象的引用传递给对象加载器。如何让对象加载器在成功响应后更新 userId? (我希望能够做到这一点,而不必捕获响应本身。)

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 = @"[email protected]";
  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\":\"[email protected]\",\"userId\":5,\"lastName\":\"Morris\",\"phone\":\"\",\"timeStamp\":3987987968.0}}"

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.)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

孤蝉 2024-12-31 19:06:25

是什么让您认为一旦 postObject 成功,新的 User 对象就不会被更新?

我的理解是,当您 postObject 时,在 RKObjectLoader 中,它会尝试将响应映射回正在 POST 的原始对象,作为默认行为。让它不这样做的唯一方法是对目标对象进行计费。

尝试这样的事情:

[[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 } ) 那么它应该“正常工作”。

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

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.

Try something like this:

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

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".

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