跟踪NSManagedObject的属性更改

arn*_*app 4 core-data nsmanagedobject nsmanagedobjectcontext ios

我正在寻找一种方法来跟踪NSManagedObject的属性更改.

目前我使用NSNotifactionCenter来查看我的managedobjectcontext的更改:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleDataModelChange:) name:NSManagedObjectContextObjectsDidChangeNotification object:self.managedObjectContext];
Run Code Online (Sandbox Code Playgroud)

它会触发handleDataModelChange Methode,如下所示:

- (void)handleDataModelChange:(NSNotification *)note
{
    NSSet *updatedObjects = [[note userInfo] objectForKey:NSUpdatedObjectsKey];

    if (updatedObjects.count > 0) {
        for (NSManagedObject *obj in updatedObjects.allObjects) {
            NSLog(@"Object updated: %@ with values:",obj.entity.name);
            NSDictionary *theAttributes = [self getAllAttributesOf:obj];
            for (NSString *attributeName in theAttributes) {
                NSLog(@"Name: %@ : %@",attributeName,[obj valueForKey:attributeName]);
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

如果对象发生更改,则会记录该对象的新属性.如何获得获取旧属性值的方法?

Tim*_*ter 6

NSManagedObject类参考:

changedValues

返回一个字典,其中包含自上次获取或保存接收器以来已更改的持久属性的键和(新)值.

changedValuesForCurrentEvent

返回一个字典,其中包含自上次发布以来已更改的持久属性的键和旧值NSManagedObjectContextObjectsDidChangeNotification.

  • changedValues返回一个带有一个键的字典,该键包含关系的新值,但是changedValuesForCurrentEvent返回一个空集。有任何想法吗? (2认同)