Skip navigation.
 
mlRe: CoreData retain/release/delete
FROM : Scott Stevenson
DATE : Fri Apr 29 22:03:59 2005

On Apr 29, 2005, at 12:49 PM, John Brownlow wrote:

> That almost answers my question.
>
> But I am concerned about managedObjects whose retain count falls to
> zero, but which are *not* explicitly deleted using -deleteObject.


You really don't need to worry about stuff like that. The context
handles the availability of the actual objects in memory.


> Are these automatically deleted from the persistent store on save, or
> do they hang around?


If you delete an object and save, I goes away from the persistent store
and eventually gets dealloc'd.


> I create a lot of objects with many relationships which I do not want
> to track manually. Once they are released from all relationships and
> their retain count falls to zero, I want them to be deleted from the
> persistent store since I have no further use for them.


This gets a lot of easier of you don't think about retain counts at
all. In fact, an object at a particular place in memory could
theoretically be released (and later re-alloc'd) with any deletion at
all. Core Data abstracts you from the process of basic memory
management.


> An example of this might be a 'sequence' object, which is an ordered
> collection of 'scene' objects. Once a sequence has lost all of its
> scenes and has been removed from any parent sequences, its
> retain-count will be zero and I would like it to be removed from the
> persistent store on the next save


Hmmm... typically you want to use cascading, but it sounds like it
probably won't work here.

Your best bet might be to create a custom subclass of NSManagedObject
and implement -removeScenes: and/or -removeSceneObject: (don't hold me
to the exact names). Core Data will call these to remove scenes from
the relationship.

Inside that method, you could check to see if there will be any more
scenes after the current set is removed. If not, you can delete 'self'.
This isn't incredibly elegant, but I'm not aware of a better solution.

You can control-click on an Entity in the model to get a custom class
generated for you. Should be easier to figure this by doing that.


    - Scott

--
http://treehouseideas.com/
http://theocacao.com/ [blog]

Related mailsAuthorDate
mlCoreData retain/release/delete John Brownlow Apr 29, 20:53
mlRe: CoreData retain/release/delete Scott Stevenson Apr 29, 21:07
mlRe: CoreData retain/release/delete John Brownlow Apr 29, 21:49
mlRe: CoreData retain/release/delete Scott Stevenson Apr 29, 22:03
mlRe: CoreData retain/release/delete John Brownlow Apr 29, 22:22
mlRe: CoreData retain/release/delete Scott Stevenson Apr 29, 22:29
mlRe: CoreData retain/release/delete Bill Bumgarner Apr 29, 23:46
mlRe: CoreData retain/release/delete Johnny Deadman Apr 30, 02:02
mlRe: CoreData retain/release/delete Scott Stevenson Apr 30, 02:09
mlRe: CoreData retain/release/delete mmalcolm crawford Apr 30, 02:10
mlRe: CoreData retain/release/delete Johnny Deadman Apr 30, 02:16
mlRe: CoreData retain/release/delete Shawn Erickson Apr 30, 02:21
mlRe: CoreData retain/release/delete mmalcolm crawford Apr 30, 02:23
mlRe: CoreData retain/release/delete Shawn Erickson Apr 30, 02:32