Re: CoreData, reset a persistent store
Re: CoreData, reset a persistent store
- Subject: Re: CoreData, reset a persistent store
- From: Pierre Chatelier <email@hidden>
- Date: Wed, 15 Nov 2006 20:37:54 +0100
Any chance you can share just that little snippet that handles the
delete routine?
Sure !
Here it is :
//first version, fetch all, deletes, save
[managedObjectContext lock];
NSFetchRequest* fetchRequest = [[NSFetchRequest alloc] init];
[fetchRequest setEntity:[NSEntityDescription
entityForName:@"CachedFeatures"
inManagedObjectContext:managedObjectContext]];
[fetchRequest setPredicate:[NSPredicate
predicateWithFormat:@"SELF != nil"]];
NSArray* allObjects = [managedObjectContext
executeFetchRequest:fetchRequest error:&error];
NSLog(@"[allObjects count] = %d", [allObjects count]);
NSEnumerator* enumerator = [allObjects objectEnumerator];
id object = nil;
while((object = [enumerator nextObject]))
[managedObjectContext deleteObject:object];
[managedObjectContext save:&error];
[managedObjectContext unlock];
//second version : delete file
[managedObjectContext lock];
[managedObjectContext reset];//to drop pending changes
if ([persistentStoreCoordinator
removePersistentStore:persistentStore error:&error])
{
NSURL* storeURL = [NSURL fileURLWithPath:[self
pathForPersistentStore]];
[[NSFileManager defaultManager] removeFileAtPath:[storeURL
path] handler:nil];
[self addPersistentStore];//recreates the persistent store
}
[managedObjectContext unlock];
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden