I am fetching an object from the persistentStoreManagedObjectContext
and showing some of its value to my user within a UIView. That object, let's call it Book
has an Author
relationship. Whenever I am about to display that book, I check if the author is set or not. If it's not set, I do:
[[RKObjectManager sharedManager] getObjectsAtPath:[NSString stringWithFormat:@"/api/rest/userprofiles/%@/",_currentBook.authorID] parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {..}
Within the success callback, I want to do a:
_currentBook.author = mappingResult.firstObject;
if (![_currentBook.managedObjectContext saveToPersistentStore:&error]) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
}
However, I can't do _currentBook.author = mappingResult.firstObject;
because I get:
Illegal attempt to establish a relationship between objects in different contexts
However, I know that this newly fetched Author
is saved within the persistentStoreCoordinator, because I configured my RestKit to do so (or it's by default, I can't remember). So I don't want to create that object AGAIN, I would just want to get it's value within my currentContext which is persistentStoreManagedObjectContext
. Do I have to use a NSFetchedResultsController just for that?
Edit
I already have a connection in the book to connect to the author, via the authorID, which I send back at the same time as getting the Book object from my server. However, it might be the case that the Author Object is not fetched yet, hence I keep that ID.
[bookMapping addConnectionForRelationship:@"author" connectedBy:@{ @"authorID": @"identifier" }];
However, even after that author is fetched, book.author is set to null, unless I REFETCH once again in the persistent storage.