Search code examples
objective-ccocoa-touchrelease-managementnsmanagedobject

Retaining Managed objects - more general retaining objects


A quick question regarding Managed Objects.

I created an Array with Managed Objects (in Object 1: TableViewConbtroller), and pass one of those objects to another class/object (object 2: TableCell).

The original array should still be retained in the original caller class. Then Object 2 is released, does that mean that that particular item in the array is released as well, as the reference to it in Object 2 was released?

I am trying to better understand how to work with ManagedObjects as I get 'Object was released' errors.

[EDIT] After some experimenting I came across the following scenario: I have the main AppDelegate. In a different class I create an AppDelegate to obtain the ManagedObjectContext.

appDelegate = (iDomsAppDelegate *)[[UIApplication sharedApplication] delegate];
[self setContext:[appDelegate managedObjectContext]];

When the class is finished, and I release it, the variable in the class 'appDelegate' is also released. But then the ManagedObjectContext is closed, and obvious any future attempt to use it will cause a crash. So should I leave the appDelegate unreleased?

This comes to the same question as the above about when and how to release in those situations where an objects is used from another class. I think a way of putting it is, how to know when you own an object and when not.


Solution

  • The UIApplication retains your app delegate, so releasing it in your view controller's dealloc method will not deallocate the app delegate.