NSManagedObject's hasChanges is true while changedValues is empty

后端 未结 5 997
心在旅途
心在旅途 2021-02-12 22:41

I am trying to observe individual NSManagedObject changes on NSManagedObjectContextWillSaveNotification:

- (void)managedObjectContextWi         


        
5条回答
  •  醉话见心
    2021-02-12 23:21

    In my experience, if the entity already existed, you loaded it and then you set a value to a property that is equal to its previous value, then the record will be marked as updated, hasChanges will return YES, and changedValues will be empty. When you save the context, what gets updated is a special Core Data column called Z_OPT, which refers to the number of times an entity has been updated. For these situations you can do something like this before saving:

    for (NSManagedObject *managedObject in context.updatedObjects.objectEnumerator) {
        if (!managedObject.changedValues.count) {
            [context refreshObject:managedObject mergeChanges:NO];
        }
    }
    

    in order to don't even update the Z_OPT value.

提交回复
热议问题