0

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.

abisson
  • 4,365
  • 9
  • 46
  • 68

1 Answers1

0

You should get RestKit to connect the relationship using foreign key mapping based on the author id. That way all of the updates are made at the same time, in the same context, and saved before the mapping result is returned.

Wain
  • 118,658
  • 15
  • 128
  • 151
  • Please see edit. I am already doing that, but I am not keeping an inverse from author to book, as author is essentially a User, which is related to to many other things. – abisson Jul 22 '13 at 19:20