保留托管对象 - 更一般的保留对象
关于托管对象的一个简单问题。
我创建了一个包含托管对象的数组(在对象 1:TableViewConbtroller 中),并将其中一个对象传递给另一个类/对象(对象 2:TableCell)。
原始数组仍应保留在原始调用者类中。 然后对象 2 被释放,这是否意味着数组中的该特定项也被释放,因为对象 2 中对其的引用被释放?
当我收到“对象已发布”错误时,我试图更好地理解如何使用 ManagedObjects。
[编辑] 经过一些实验,我遇到了以下场景: 我有主要的 AppDelegate。 在另一个类中,我创建一个 AppDelegate 来获取 ManagedObjectContext。
appDelegate = (iDomsAppDelegate *)[[UIApplication sharedApplication] delegate];
[self setContext:[appDelegate managedObjectContext]];
当该类完成并释放它时,“appDelegate”类中的变量也被释放。但随后 ManagedObjectContext 被关闭,显然任何未来使用它的尝试都会导致崩溃。那么我应该不释放 appDelegate 吗?
这与上面的问题相同,即在从另一个类使用对象的情况下何时以及如何释放。我认为一种说法是,如何知道你何时拥有一个物体,何时不拥有一个物体。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
UIApplication 保留您的应用程序委托,因此在视图控制器的 dealloc 方法中释放它不会释放应用程序委托。
The UIApplication retains your app delegate, so releasing it in your view controller's dealloc method will not deallocate the app delegate.