1

I noticed that the fault for a to-one relationship fires immediately when the fetch request is executed even though I'm not accessing the relationship. Particularly, a breakpoint in the method newValueForRelationship:forObjectWithID:withContext:error: is hit immediately in the NSIncrementalStore subclass.

- (id)newValueForRelationship:(NSRelationshipDescription *)relationship
          forObjectWithID:(NSManagedObjectID *)objectID
              withContext:(NSManagedObjectContext *)context
                    error:(NSError **)error

https://developer.apple.com/library/mac/documentation/CoreData/Reference/NSIncrementalStore_Class/index.html#//apple_ref/occ/instm/NSIncrementalStore/newValueForRelationship:forObjectWithID:withContext:error:

The breakpoint is only hit when I access the relationship as expected for to-many relationships as expected (lazy loaded).

Can someone please help?

pshah
  • 2,052
  • 1
  • 21
  • 40

1 Answers1

0

This is all about designing your own NSIncrementalStore. Please go through doc Incremental Store Programming Guide.

Here are extracts from the guide:

If the backing data store makes to-one relationships readily available, your incremental store returns to-one relationships in newValuesForObjectWithID:withContext:error:. If your app and data store are more efficient when all relationship fetching is deferred, your store may instead return them in newValuesForRelationship:forObjectWithID:withContext:error:.

dev gr
  • 2,409
  • 1
  • 21
  • 33
  • I get that but I'm not sure how this answers my question. – pshah Dec 09 '14 at 16:48
  • Try setting breakpoint in newValuesForObjectWithID: and see if it is hit for the same to-one relationship, hopefully you will get your answer. – dev gr Dec 09 '14 at 16:54
  • That breakpoint gets hit for every object even if a relationship is not accessed. That is my understanding. And, I am able to verify that this is the behavior. – pshah Dec 09 '14 at 17:51
  • Did it hit for same to-one relationship you were verifying? If yes, then you can deduce the fact that Core Data trying to make to-one relationship object readily available. – dev gr Dec 09 '14 at 17:57
  • newValuesForObjectWithID:withContext:error: is not hit for a relationship. But, as per the document, it allows setting to-one relationships. If I set the to-one relationship to [NSNull null] in newValuesForObjectWithID:withContext:error: then the newValuesForRelationship:forObjectWithID:withContext:error: method is not called. So far it seems like the CoreData wants to make to-one relationships readily available through newValuesForRelationship:forObjectWithID:withContext:error: or newValuesForObjectWithID:withContext:error: But, it can't be deferred like to-many relationships. – pshah Dec 09 '14 at 18:16