Core Data Confusion

Marcus S. Zarra mzarra at mac.com
Mon Feb 5 09:04:39 PST 2007


The KVO methods do not fire faults in Core Data.  Core Data retrieves  
all of the values for a managed object when that object is accessed.   
It specifically does not do any lazy initialization of attributes.   
However, it does do lazy initialization of relationships.

The primitive methods will cause a fault to fire on a lazy  
relationship.  If you access another managed object via - 
primitiveValueForKey: and that object has not been loaded yet -- the - 
primitiveValueForKey: method will cause it to fire.  This can be  
tested very easily.

The -willChangeValueForKey: and -didChangeValueForKey: should be  
wrapped around any calls to primitive setters -- not getters.  The  
will/did methods cause a KVO notice to be sent out to let other  
observers know that the value has changed.  It does not make sense to  
fire the will/did when you are reading the value.  In the example the  
OP posted, the will/did are not necessary and accessing the values in  
the same managed object via the primitive methods is safe and makes  
sense in some situations.

Marcus S. Zarra
Zarra Studios LLC
Simply Elegant Software for OS X
www.zarrastudios.com

On Feb 5, 2007, at 6:40 AM, Christiaan Hofman wrote:

> I think the OP meant the remark about KVO the other way around: he  
> seems to think that the report is called only because KVO told that  
> the underlying data has changed. At least that is what I  
> understood, and remarked this assumption is generally incorrect.
>
> Generally, when accessing properties managed by CD, one should use  
> the proper non-primitive methods or wrap will/didAccessValueForKey  
> methods around the primitive accessors. That's what the docs say,  
> and I assume that's for a reason. The reason I can think of is that  
> the will/didAccess.. methods make sure to fire the fault (fetch/ 
> update the value) if necessary. That would make sense to me,  
> knowing that CD doesn't fetch properties immediately. Anyway, I  
> don't know what it does, and I don't need to know. The only thing I  
> need to know is what is in the docs: you better do it make sure the  
> will/didAccess... methods are called. HOWEVER, if you choose to use  
> primitive accessors, you are responsible to make sure everything is  
> set up right. When you also don't use the will/didAccess...  
> methods, you have to make sure that everything these methods does  
> (like faulting,etc ?) has been taken care of. This DOES mean you  
> need to know what these methods do (and you don't, unless you work  
> for Apple, but also that is not true, as some framework developer  
> might need to subclass these methods).
>
> So to conclude. Can it hurt? The answer is: Yes.
>
> Christiaan



More information about the MacOSX-dev mailing list