处理 NSArray 时如何避免保留循环(默认保留对象)?
我的情况是我有两个班级:“人”和“组”。 每个人都有一个 NSArray 来恢复它所属的每个组。 在每个组中,它都有一个 NSAraay,恢复每个人属于它的状态。
并且一个人可以属于多个组,一个组可以有很多人属于它。
我认为这是一种保留计数,一个人保留一个 NSArray,该 NSArray 保留该组,该组保留一个 NSArray,该组保留回该人。
但我认为我确实需要每个人都知道它属于哪个群体,并且每个群体都知道这些人属于它。
我该如何解决这个问题?
我认为 NSArray 不会保留所有会打破循环的成员,但显然 NSArray 必须保留它们。所以请提供一些建议。
My situation is that I have two Classes : "Person" and "Group".
In every person, it has a NSArray that restores every group it belong to.
In every group , it has a NSAraay that restores every person belongs to it.
And one person can belong to many group and one group can have many people belongs to it.
And This is a kind of retain count I think,A person retains a NSArray which retains the Group Which retains a NSArray which retains back to the person.
But I think I do need every person knows which group it belongs to and every group knows the persons belongs to it.
How can I solve this?
I think it the NSArray doesn't retains all it's members that would break the cycle but obviously NSArray must retains them.So please provide some advice.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用
[NSValue valueWithNonretainedObject:...]
将对象放入一个数组而不保留它们。我不确定这是否是最好的解决方案,但它会起作用。You can use
[NSValue valueWithNonretainedObject:...]
to put your objects into an array without retaining them. I'm not sure if this is the best solution, but it will work.一般来说,您需要一个弱参考系统来实现这一点。您可以使用非保留 CFArray,但绕过保留/释放系统是危险的。这是 NSMutableArray 上的一个快速而肮脏的类别,可以实现这一点。
对于您的具体情况,我会退后一步,看看是否有更好的方法来构建您的类以完全规避这个问题。
In general, you would need a weak referencing system to achieve this. You can use a non-retaining CFArray, but bypassing the retain/release system is dangerous. Here is a quick-and-dirty category on NSMutableArray to allow for this.
For your specific case, I would take a step back and see if there isn't a better way to structure your classes to circumvent this problem entirely.
在阅读问题后,您似乎需要在应用程序启动期间保留这些数据(我可能完全错了)。如果这是真的,你应该看看 CoreData &为 Person & 创建数据库实体团体。在这种情况下,CoreData 将管理您的所有参考资料和信息。您可以获取“团体中的人员”&使用类似 SQL 的查询来“一群人”。
如果这不是真的,我想到的一个简单的解决方案是存储间接引用而不是直接引用。例如,每个人或组都应该有一个 ID(假设它是一个
NSString
)。然后,每个 Person 类将存储一个 NSString 数组(与其所属组相对应的组 ID)&每个 Group 类将存储一个 NSString 数组(与其成员对应的人员 ID)。这样,您将需要维护一个 Map/Dictionary 结构,用于存储
& 等对。在此结构中查找 ID。It seems after reading the question that you need to persist this data across app launches (I might be completely wrong). If this is true, you should have a look at CoreData & create database entities for Person & Group. In that case, CoreData will manage all your references & you can fetch 'persons in a group' & 'groups of a person' using SQL like queries.
If this is not true, a simple solution that comes to my mind is to store indirect references rather than direct references. For example, each person or group shall have an ID (let's say it is an
NSString
). Then, each Person class will store an array ofNSString
s (group IDs corresponding to groups to which it belongs) & each Group class will store an array ofNSString
s (person IDs corresponding to its members). With this, you will need to maintain a Map/Dictionary structure which stores pairs like<personOrGroupID, PersonOrGroupObject>
& look up the IDs in this structure.