This is what I have used for years with good success:

- (BOOL)retainedObjectHasBeenDeleted
{
        // if object has been deleted, then it no longer exists
        if ([self isDeleted]) return YES;       
        // otherwise, see if object with this ID exists in the database
        NSManagedObjectContext *context = [self managedObjectContext];
        if (context == nil) return YES;
        NSManagedObjectID *objectID = [self objectID];
        if (objectID == nil) return YES;
        NSManagedObject *obj = [context objectRegisteredForID:objectID];
        return obj == nil;
}

I'm currently experimenting with the following to see if it's as safe and 
perhaps faster:

- (void)prepareForDeletion
{
        // track object deletion for faster testing below
        NSString *objIDStr = [[[self objectID] URIRepresentation] 
resourceSpecifier];
        [_deletedObjectIDs addObject:objIDStr];
}

- (BOOL)retainedObjectHasBeenDeleted
{
        NSString *objIDStr = [[[self objectID] URIRepresentation] 
resourceSpecifier];
        return [_deletedObjectIDs containsObject:objIDStr];
}

Unfortunately _deletedObjectIDs grows without bound. So you wouldn't want to 
use this approach if you are expecting a lot of deletions.

_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to