我应该在哪里释放对象?它能自行释放吗?
我想从 Facebook 获取好友信息并用 Core Data 保存。
我构建了一个 FriendsManager 类,它从 Facebook 获取好友列表(姓名和 ID) 和一个 FriendInfo 类,它获取 id 并获取有关该朋友的所有信息。
我想我会这样做:
- 创建一个 FriendsManager 对象,让它获取我所有的朋友(不是真正的问题);
- 在 FriendsManager 中创建 FriendInfo 实例;
- 给它 ID,让它获取我需要的所有信息,包括家乡的地理编码;
- 用核心数据保存所有这些。
那么我该在哪里释放这个对象呢?
为了获取所有朋友的信息,我将在循环中创建这些对象,该循环使用朋友 ID 遍历我的数组。
我还负责获取所有信息和信息。使用 Grand Central Dispatch 进行地理编码,因此并不真正知道 FriendInfo 何时获得所需的所有数据并准备就绪。
I want to get friends information from Facebook and save it with Core Data.
I build a FriendsManager Class that gets the a list of friends from Facebook (name and id)
and a FriendInfo Class that takes the id and gets all the info about this friend.
I thought I'd do it this way:
- Create an object of FriendsManager let it get all my friends (not really a problem);
- in the FriendsManager create an instance of FriendInfo;
- give it the id and let it get all the info I need including geocoding of hometown;
- save all this with core data.
But then where do I release this object?
In Order to get all the friends info I would create these objects in an loop that goes through my Array with friend ID's.
I also do all the getting info & geocoding with Grand Central Dispatch so do not really know when a FriendInfo got all the data it needs and is ready.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Core Data 对象通常是 NSManagedObject 的子类,而不是 NSObject。您不分配或释放它,而是使用 ManagedObjectContext 创建一个对象上下文,然后保存或删除它,但不释放它,因为您不拥有它。
NSObject(ARC之前的)遵循内存管理的通常规则——简单来说,如果你通过alloc、copy或以new开头的方法获得它,它就是你的,当你不再使用它时,你需要使用release。如果您在没有分配它的情况下获取它 - 例如
"NSString *string = [NSSTring stringWithFormat:@"test"];
那么它会自动释放,您不需要释放它。相信分析器 - 。
听起来你可能想要定义你的核心数据模型,然后为朋友信息创建一个 NSManagedObject 子类,你可以使用类似
[NSEntityDescription 的东西来创建它 insertNewObjectForEntityForName:@"FriendInfo" inManagedObjectContext:moc];
而不是像 NSObject 那样的[[FriendInfo alloc] init]
思维的转变是创建对象。并由 ManagedObjectContext 进行管理,在您将任何数据放入其中之前,您请求一个新的数据并放入数据,然后通知 ManagedObjectContext 您想要对该对象执行什么操作 - 保存或回滚(撤消自上次以来的所有更改) 节省)。A Core Data object is usually a subclass of NSManagedObject, not a NSObject. You don't alloc or release it, you create one with the ManagedObjectContext and you either save or delete it but you don't release it because you don't own it.
A NSObject (prior to ARC) follows the usual rules of memory management - simply, if you get it through alloc, copy or a method starting with new, it is yours and you need to use release when you no longer use it. If you get it without allocating it - for example
"NSString *string = [NSSTring stringWithFormat:@"test"];
then it is autoreleased and you don't need to release it.Trust the analyser - shift-command-B.
Sounds like you might want to define your core data model and then create a NSManagedObject subclass for the friends information. You would create it using something like
[NSEntityDescription insertNewObjectForEntityForName:@"FriendInfo" inManagedObjectContext:moc];
rather than[[FriendInfo alloc] init]
as you would if it were a NSObject. The shift in thinking is that the object is created and managed by the ManagedObjectContext before you put any data in there, you request a new one and put in the data, then inform the ManagedObjectContext what you want done with that object - save or rollback (reverse all changes since the last save).你所描述的问题比较“高层次”,很难给出具体的建议。但一般来说,您可能只想使用 ARC,而不用担心您提到的大多数内存管理问题。
另外,由于您提到要使用核心数据,因此您不会释放要保存的任何对象,因为核心数据保存了上下文的所有对象,因此它们永远不会被“释放”。
您建议的使用 GeneralManagerClass 检索所有信息并填充朋友的新实例的方法通常是可以的。我也是这样做的,这样思考起来就相对容易了。
所以我会从它开始,然后提出更具体的问题。目前,如果您按照您建议的路线进行操作,我确实没有看到您提到的问题有任何一般困难......
The problem you explain is rather "high-level" and it is difficult to give concrete advice. But in general you might want to just use ARC and don't hassle with most of these memory management problems you mentioned.
Also since you mentioned that you want to use core data, you would not release any of the objects you want to save since core data saves all objects of the context and so they never get "freed" anyway.
Your suggested approach about using a GeneralManagerClass to retrieve all the info and fill new instances of friends is generally OK. I also do it this way and it makes thinks relatively easy.
So I would just start with it and then ask more concrete questions. For now, I really don't see any general difficulty in the issues you mentioned if you follow the route you suggested...