2
votes

How to release a NSManagedObject object? When I deleted an object(NSManagedObject) in NSManagedObjectContext using - deleteObject: and than perform save action, But I do not see, It's released. DO I need to care about NSManagedObject Memory Management?

I am using the ARC in this project and the managed object overriding the dealloc method. the code looks like

  • (void)dealloc { NSLog(@"Managed Object Released") }

I delete a managed object from context and then save indicate I don't need this managed object, If the context does not release it whether waste the memory?

Thanks in advance.

2
But you overridden dealloc! you just denied the OS the opportunity to dealloc your object. In your override, please call [super dealloc] and test again. - Motti Shneor

2 Answers

2
votes

Short answer - Core Data will manage the memory for NSManagedObject objects.

Longer answer - Even if Core Data has been told to delete the object from the context, you could still be retaining a reference to the (now invalid) object yourself somewhere, say in an array that contained your original fetched results, or maybe as a property. As you are using ARC those objects will eventually be released, and so will any reference they have to your object, but you might not see a dealloc right away when you call deleteObject: for this reason.

If you're not having performance or memory issues, I wouldn't worry too much about it. If you are then it might be worth taking a look at the refreshObject: mergeChanges: method of NSManagedObjectContext as a starting point.

0
votes

In a typical scenario where you fetch a managed object from a context, then delete it and then save the context, you do not need to release the managed object as long as you are not sending retain to it, of course.

PS: If your question motivation is learning that's fine, but I would recommend you to move to ARC.