views:

94

answers:

1

Hello,

I keep track of my 'objects' using the isUpdated instance method of NSManagedObject Class.

When I'm modifying an exisiting object, it works.

If I create a new object using for example:

[NSEntityDescription insertNewObjectForEntityForName:@"Entity" inManagedObjectContext:managedObjectContext]

I can't use the isUpdated, I have to use the isInserted.

This works, but what I want to check, if the object has been modified with new data.

isInserted will return FALSE no matter if the object has been changed or not, it only take care if has been inserted or not ...

what can I use ? I can track the initial state of the object properties but I would prefer the isUpdated approach.

thanks!!!

r.

A: 

I'm not sure i completely understand your question, however, if you want to check whether your working with an unsaved new NSManagedObject, you can do that by writing a small category for NSManagedObject:

@interface NSManagedObject(Utility)

/**
 Returns YES if this managed object is new and has not yet been saved in the persistent store.
 */
- (BOOL)isNew;

@end

@implementation NSManagedObject(Utility)

- (BOOL)isNew {
    NSDictionary *vals = [self committedValuesForKeys:nil];
    return [vals count] == 0;
}

@end

If you've created a new managed object using:

[NSEntityDescription insertNewObjectForEntityForName:@"Entity" inManagedObjectContext:managedObjectContext]

You can use the -isNew method to check whether it has been saved or not.

Mustafa