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: Thu, 16 Nov 2006 09:07:09 +0100
So at this point you really want to discard all the data in the
store (and don't care about any metadata in the store)?
I want to delete in the persistent store all the objects that have
been put inside by a known NSManagedObjectModel
Can you structure your application in such a way that at this
point, you tear down your persistence stack (persistent store
coordinator, managed object model, and any managed object
contexts), remove the file on disk, then bring the persistence
stack back up?
(1) Make sure you have no existing instances of any managed objects
associated with that persistent store.
(2) Ask any persistent store coordinator to which your persistent
store has been added to remove it.
(3) Delete the underlying file corresponding to the persistent store.
Then you can add a new persistent store to any coordinator with the
same URL and get a new, empty persistent store at that URL as a
result.
If you look at my first posts, this is exactly the solution that I
chose, because it was very efficient. I began this thread of
discussion because I was wondering if it was the only way to make it
efficient.
[managedObjectContext lock];
[managedObjectContext reset];
if ([persistentStoreCoordinator
removePersistentStore:persistentStore error:&error])
{
NSURL* storeURL = [NSURL fileURLWithPath:[self
pathForPersistentStore]];
[[NSFileManager defaultManager] removeFileAtPath:[storeURL
path] handler:nil];
[self addPersistentStore];
}
[managedObjectContext unlock];
Setup the delete rule so that when the "master" object is deleted,
it cascades to all of the other objects
Interesting idea ! Should be tried ! But I have no "master" object.
It means that I should modify my data model to workaround a method I
miss in the CoreData API. I was told to take care of "premature
optimization", but at least, by deleting the file directly, I was not
breaking my program as a workaround ;-)
But I will try in the future.
You're thinking about this the wrong way round; managed objects do
not represent what's in the store, the store is a persistent
repository for managed objects...
Ok, I understand the problem. There could be several
ManagedObjectContext associated to the persistent store, so I just
cannot tell "remove managed objects" because managed objects have
sense only in a context, i.e. they have been fetched...
But in that case, I would like to tell the persistent store "forget
all object of a given entity". And there is no method like that.
Personnally, I am satisfied with the discussion, and I think it can
be closed. I would like to sum up the different points we've talked
about :
-Fetching an object will always result in time and memory consumption
since even faulted, the fetched data is cached.
-So far, a persistent store cannot be requested unless we use a
managed object context (which implie a fetch). So, an "object" of the
persistent store has no meaning until it is actually fetched by a
context.
-My request would be to Apple, to allow interaction with the
persistent store, either with SQLLite requests, or with a minimal set
of function like "removeAllEntities:(NSEntityDescription*) cascade:
(BOOL)". This could be used to clear the persistent store without
fetching in a managed object context.
-So far, a solution to my innitial problem (clearing a persistent
store as minimal cost), is to remove by hand the underlying file.
Thanks to everybody
Regards,
Pierre Chatelier
_______________________________________________
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