Note that there are some explanatory texts on larger screens.

plurals
  1. POGet the MappingResult firstObject value within the Persistent Storage to avoid "Illegal attempt to establish relationships" errors
    primarykey
    data
    text
    <p>I am fetching an object from the <code>persistentStoreManagedObjectContext</code> and showing some of its value to my user within a UIView. That object, let's call it <code>Book</code> has an <code>Author</code> 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:</p> <pre><code> [[RKObjectManager sharedManager] getObjectsAtPath:[NSString stringWithFormat:@"/api/rest/userprofiles/%@/",_currentBook.authorID] parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {..} </code></pre> <p>Within the success callback, I want to do a:</p> <pre><code> _currentBook.author = mappingResult.firstObject; if (![_currentBook.managedObjectContext saveToPersistentStore:&amp;error]) { NSLog(@"Unresolved error %@, %@", error, [error userInfo]); } </code></pre> <p>However, I can't do <code>_currentBook.author = mappingResult.firstObject;</code> because I get:</p> <pre><code>Illegal attempt to establish a relationship between objects in different contexts </code></pre> <p>However, I know that this newly fetched <code>Author</code> 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 <code>persistentStoreManagedObjectContext</code>. Do I have to use a NSFetchedResultsController just for that?</p> <p><strong>Edit</strong></p> <p>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.</p> <pre><code>[bookMapping addConnectionForRelationship:@"author" connectedBy:@{ @"authorID": @"identifier" }]; </code></pre> <p>However, even after that author is fetched, book.author is set to null, unless I REFETCH once again in the persistent storage.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload