Re: Core Data: Determine if managed object is deleted
Re: Core Data: Determine if managed object is deleted
- Subject: Re: Core Data: Determine if managed object is deleted
- From: David Riggle <email@hidden>
- Date: Fri, 21 Oct 2011 08:57:19 -0700
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 (email@hidden)
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:
This email sent to email@hidden