The issue:
I'm reading Tim Roadley's "Learning Core Data of iOS". I don't understand why the author takes an extra, seemingly superfluous step when retrieving a managed object from a fetched results controller.
NSManagedObjectID *itemId = [[self.fetchedResultsController objectAtIndex:indexPath] objectID];
// Item is a NSManagedObject subclass
Item *item = (Item *)[self.fetchedResultsController.managedObjectContext existingObjectWithID:itemId error:nil];
// gets and sets item's attributes…
Isn't that weird? Why go to the trouble of using the objectID
and the existingObjectWithID:error:
method rather than just use the fetched results controller's objectAtIndexPath:
method to retrieve the item object?
What I have done to try and understand:
(1.) I searched the entire electronic version of the book for the existingObjectWithID:error:
method. The method is used throughout the book, but I couldn't find an explanation for its usage.
(2.) I read the Apple docs on existingObjectWithID:error:
If there is a managed object with the given ID already registered in the context, that object is returned directly; otherwise the corresponding object is faulted into the context. This method might perform I/O if the data is uncached. Unlike objectWithID:, this method never returns a fault.
The object may be "faulted into the context", but the method "never returns a fault". Isn't that contradictory? Suffice it to say that the docs aren't helping me wrap my head around this.
(3.) I thought about it for a day, still no epiphany. However, I suspect the answer has something to do with saving in the background, which will be added to the book's demo app later.