views:

16

answers:

2

I am importing a bunch of data from a web service on a background thread and I randomly run into bad access problems, I believe mostly from assigning relationships.

I am wondering how long the new entities I create and save hang around?

A: 

They are released when the retain count = 0 .

Unless you take ownership of them somehow they will vanish when the autorelease pool flushes. This is assuming you are creating them with... insertNewObjectForEntityForName:inManagedObjectContext:

The managed object context will still have the record but the NSManagedObject obeys the same retain/release rules as any other object. Think of NSManagedObject as a runtime container for the underlying data.

If you want to keep a hold of them use an NSArrayController for OS X or a NSMutableSet for UIKit are suggestions

Warren Burton
thanks - that is exactly what I am doing. I just didn't know if say 1000+ entities got created some would get released due to memory constraints or something like that before I even got to the [pool release].
Slee
A: 

Core Data follows the same rules as any other object in Objective-C except with a slight twist.

Core Data will decide internally when to release the objects, so even if you are done with an object and you have released all of your references to it, it may stay around after that depending on what Core Data decides to do with it.

In addition, when a NSManagedObject is loaded into memory the entire object may not necessarily be loaded. Core Data, by default, will load the basic structure of the object but will not load in its data or relationships. This keeps the initial memory footprint very low. When you access a property then the entire instance will be fully realized.

The short answer is this: As long as you follow the retain release rules you do not need to worry about memory management with Core Data.

Marcus S. Zarra