setPrimitiveValue forKey does not register a NSManagedObject with changes
| Originator: | Jake.Kirshner | ||
| Number: | rdar://15516199 | Date Originated: | 20-Nov-2013 12:22 PM |
| Status: | Open | Resolved: | |
| Product: | iOS SDK | Product Version: | 7.0.4 |
| Classification: | Crash/Hang/Data Loss | Reproducible: | Always |
Summary: I am updating a set of objects using a background NSManagedObjectContext, which I then import into a primary MOC. when updating a int32_t (or 16-bit) field using setPrimitiveValue forKey where I pass in an NSNumber @(11), the ManagedObject returns the new value properly, but a call to hasChanges returns NO. Steps to Reproduce: 1) create a bunch of ManagedObjects in a MOC. 2) in a separate thread create a new MOC with the same persistentStoreCoordinator. 3) update an int32_t property in the objects using the new MOC and check the results of the hasChanges method. 4) using NSNotificationCenter import merge the changes in the background MOC into the primary MOC and check the value of the int32_t field you updated. Expected Results: i would expect the hasChanges in the managed object to return YES. Actual Results: the hasChanges method returns NO. Regression: Describe circumstances where the problem occurs or does not occur, such as software versions and/or hardware configurations. Notes: Provide additional information, such as references to related problems, workarounds and relevant attachments.
Comments
Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!
Works as documented
See: https://developer.apple.com/library/ios/documentation/cocoa/reference/CoreDataFramework/Classes/NSManagedObject_Class/Reference/NSManagedObject.html#//apple_ref/doc/uid/TP30001171-BBCEBFCH
"This method does not invoke the change notification methods (willChangeValueForKey: and didChangeValueForKey:). It is typically used by subclasses that implement custom accessor methods that need direct access to the receiver’s private internal storage. It is also used by the Core Data framework to initialize the receiver with values from a persistent store or to restore a value from a snapshot."
In your scenario you would have to wrap the calls to setPrimitiveValue:forKey: with willChangeValueForKey: and didChangeValueForKey: per the documentation. This is explored in more detail in the programming guide: https://developer.apple.com/library/ios/documentation/cocoa/Conceptual/CoreData/Articles/cdAccessorMethods.html#//apple_ref/doc/uid/TP40002154-SW5