On Thu, Feb 12, 2009 at 10:55 AM, Samuel Strupp <str...@synium.de> wrote:

> I simply want to delete the actual database (SQL Database) and load a new
> one.

  Okay.

> First: all controllers which hold refs to NSManagedObjects will be deleted.

  What do you mean "deleted"? Do you just mean you're disconnecting
them from their managed object context?

> Then the i call [[NSGarbageCollector defaultCollector] collectExhaustively];

  Why? If you're going to do it at all, I'd imagine you'd do it after
the tear-down of *everything* related to your context and persistent
store.

> Then i disconnect from the database.

  Again, I'd do this *before* forcing collection.

> This is the code to disconnect from the databse:
>
> -(BOOL) removeData {
>  NSError *error;
> if (managedObjectContext != nil) {
>         if ([managedObjectContext commitEditing]) {
> if ([managedObjectContext persistentStoreCoordinator]
>      &&
> [[[managedObjectContext persistentStoreCoordinator] persistentStores] count]
>> 0) {
> if ([managedObjectContext hasChanges] &&
> ![managedObjectContext save:&error]) {
> NSLog(@"Save Error.");
> }
> }
> }
> }
> managedObjectContext = nil;
> persistentStoreCoordinator = nil;
> managedObjectModel = nil;
> return YES;
> }
>

  I don't see any code related to removing the context and
disconnecting from the persistent store. Are you really pulling the
store's file (the sqlite database) out from underneath Core Data and
expecting it to work normally? I wouldn't.

> The problem is that the disconnect and the collection of garbage is
> parallel. So the invalidate exception is thrown.

  Are you *sure* that's the problem? It may not be. My first suspect
is that I don't see anywhere that you're actually tearing down the
right parts of the Core Data stack (the context and the persistent
store).

> I have no problems with the disconnect from the old and the reconnect to new
> databse. Thats working. Only the old NSManagedObjects seems to be not
> deleted and create errors.

  Well, no, it's not really working, is it? The managed objects should
go away with your context. If they're sticking around and throwing
errors, that's your first clue that you're likely not handling the
Core Data stack properly during this disconnect process.

  Core Data lets you create and remove as many contexts as you like to
a persistent store - this is one of the features that makes Core Data
shine (and even gives it the ability to handle multithreading with
grace). If you've got managed objects hanging around after you dispose
of a context, you're "doing it wrong":

Core Data Programming Guide
http://developer.apple.com/DOCUMENTATION/Cocoa/Conceptual/CoreData/cdProgrammingGuide.html

Low Level Core Data Tutorial
http://developer.apple.com/DOCUMENTATION/Cocoa/Conceptual/CoreDataUtilityTutorial/Articles/chapter_1_section_1.html

--
I.S.
_______________________________________________

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