Reputation: 16829
How to manually set the value of a managed object property in RestKit when it's managed by the object manager ?
I created a RKObjectManager with a persistent store for core data persistence.
I added a RKEntityMapping and a RKResponseDescriptor to the object manager.
Now I can call to object manager like this :
[[RKObjectManager sharedManager]
getObjectsAtPath:@"/path_to_ressource"
parameters:nil
success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
// success
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
// report error
}];
And the data are well displayed in a UITableView (I'm using a NSFetchedResultsController for that). Everything looks fine, my data are persisted.
Now I want to add a property to the entity that depends on the keypath of the response descriptor. How should I do this and where?
My first try:
I added the property to the core data entity and then I tried this in the success block of the code presented above:
for (Entity *s in mappingResult.dictionary[@"CurrentEntities"]) {
s.isCurrent = [NSNumber numberWithBool:YES];
}
for (Entity *s in mappingResult.dictionary[@"OldEntities"]) {
s.isCurrent = [NSNumber numberWithBool:NO];
}
assuming the json looks like this:
{
CurrentEntities: [{ id: 10, title: "bhubhbhu"}, { id: 11, title: "ezeze"}, ...],
OldEntities: [{ id: 0, title: "rf-reref"}, { id: 1, title: "vcvcvcvcv"}, ...]
}
After setting the new local property to YES or NO, I can indeed see the result in my table view but it looks like those changes aren't persisted.
So any ideas?
EDIT:
Well, it seems like saving the context makes the changes persisted correctly:
NSError *error = nil;
[[RKManagedObjectStore defaultStore].mainQueueManagedObjectContext saveToPersistentStore:&error];
EDIT:
Actually it looks like I must save the context after every single object change otherwise I get Core Data errors.
Upvotes: 2
Views: 982
Reputation: 119031
As you have found, you can just save the edits you make. Because the values you're trying to set aren't based on anything in the URL used to download the data or the content of the payload data (values) your simplest option is just to post-process and save. To interact with the mapping process and inject the values will require a good deal more code.
Upvotes: 1