RestKit - 发布对象并更新其属性
我有一个使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是什么让您认为一旦 postObject 成功,新的 User 对象就不会被更新?
我的理解是,当您 postObject 时,在 RKObjectLoader 中,它会尝试将响应映射回正在 POST 的原始对象,作为默认行为。让它不这样做的唯一方法是对目标对象进行计费。
尝试这样的事情:
只要您正确设置了映射(从服务器到 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:
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".