Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to properly deal with KVO notifications when an managed object turns into a fault?

From the docs:

When Core Data turns an object into a fault, key-value observing (KVO) change notifications (see Key-Value Observing Programming Guide) are sent for the object’s properties. If you are observing properties of an object that is turned into a fault and the fault is subsequently realized, you receive change notifications for properties whose values have not in fact changed.

So if an object turns into a fault, Core Data does send KVO notifications for changed properties? So I must always check for isFault == NO before beeing happy about the notification?

like image 672
dontWatchMyProfile Avatar asked Dec 18 '22 00:12

dontWatchMyProfile


2 Answers

isFault is not reliable because isFault may still returns NO when KVO notifications are sent. One should check faultingState for that:

faultingState Returns a value that indicates the faulting state of the receiver.

  • (NSUInteger)faultingState Return Value 0 if the object is fully initialized as a managed object and not transitioning to or from another state, otherwise some other value.

Discussion The method allow you to determine if an object is in a transitional phase when receiving a key-value observing change notification.

like image 95
an0 Avatar answered Dec 19 '22 12:12

an0


That is one solution. A better solution would be to stop observing the object when it gets turned into a fault. In practice, except for in low memory situations, you know when an object is going to be turned into a fault and can plan for it.

like image 30
Marcus S. Zarra Avatar answered Dec 19 '22 14:12

Marcus S. Zarra