Re: Garbage Collection and NSManagedObject
Re: Garbage Collection and NSManagedObject
- Subject: Re: Garbage Collection and NSManagedObject
- From: Samuel Strupp <email@hidden>
- Date: Fri, 13 Feb 2009 09:48:17 +0100
You are right i forgot to remove the persistant store from the
coordinator. But i am not sure if that is important because i set the
context and coordinator refs to nil.
managedObjectContext = nil;
persistentStoreCoordinator = nil;
managedObjectModel = nil;
After this I thought the garbage collector is deleting all these
objects. Even if I remove the store from the coordinator before, I
have the same problem of INVALIDATED objects. Because this problem
occurs only sometimes (10% percent of manually started reconnects), I
thought this is a garbage collector problem.
My main question is: Is there a way to wait until the garbage
collector is finished with a "collectExhaustively"?
thanks for the help
Samuel
Am 12.02.2009 um 17:23 schrieb I. Savant:
On Thu, Feb 12, 2009 at 10:55 AM, Samuel Strupp <email@hidden>
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.
Samuel Strupp
Synium Software GmbH
Robert-Koch-Str. 50
55129 Mainz
Internet: www.synium.de
E-Mail: email@hidden
Telefon: +49 (0)6131 4970 217
Telefax: +49 (0)6131 4970 12
Handelsregister: Amtsgericht Mainz, HRB 40072
Geschäftsführer: Robert Fujara, Mendel Kucharzeck
_______________________________________________
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