简体   繁体   中英

How to update a posted object in RestKit with different primary key attributes?

I'm posting objects on a server as JSON that consists of a few attributes and an ID that is the primary key attribute. I'm also using Core Data to save all the objects locally.

The problem is that when I first create the object to POST I know all the attributes but the unique ID. The ID is set at server-side, and when I get the response from the server I have ended up with two objects in my database: One with ID 0, and one with the real ID.

Is there any way to get restkit/coredata to treat these two objects as the same, or alternatively don't save the first object in the database?

You can use the postObject: usingBlock method and assign a target object for the object loader like this.

[[RKObjectManager sharedManager] postObject:myObject usingBlock:^(RKObjectLoader *loader) {
                loader.targetObject = myObject;
                loader.delegate = self;
                loader.objectMapping = [[RKObjectManager sharedManager].mappingProvider objectMappingForClass:[myObject class]];
}

Note that you also have to set the delegate manually in the block of code every time you run the method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM