处理 NSArray 时如何避免保留循环(默认保留对象)?

发布于 2024-11-30 20:47:39 字数 331 浏览 0 评论 0原文

我的情况是我有两个班级:“人”和“组”。 每个人都有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

迷路的信 2024-12-07 20:47:39

您可以使用 [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.

墨洒年华 2024-12-07 20:47:39

一般来说,您需要一个弱参考系统来实现这一点。您可以使用非保留 CFArray,但绕过保留/释放系统是危险的。这是 NSMutableArray 上的一个快速而肮脏的类别,可以实现这一点。

@implementation NSMutableArray (WeakReferences)
    + (id)mutableArrayUsingWeakReferences {
    return [self mutableArrayUsingWeakReferencesWithCapacity:0];
    }

    + (id)mutableArrayUsingWeakReferencesWithCapacity:(NSUInteger)capacity {
    CFArrayCallBacks callbacks = {0, NULL, NULL, CFCopyDescription, CFEqual};
    // We create a weak reference array
    return (id)(CFArrayCreateMutable(0, capacity, &callbacks));
    }
@end

对于您的具体情况,我会退后一步,看看是否有更好的方法来构建您的类以完全规避这个问题。

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.

@implementation NSMutableArray (WeakReferences)
    + (id)mutableArrayUsingWeakReferences {
    return [self mutableArrayUsingWeakReferencesWithCapacity:0];
    }

    + (id)mutableArrayUsingWeakReferencesWithCapacity:(NSUInteger)capacity {
    CFArrayCallBacks callbacks = {0, NULL, NULL, CFCopyDescription, CFEqual};
    // We create a weak reference array
    return (id)(CFArrayCreateMutable(0, capacity, &callbacks));
    }
@end

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.

戴着白色围巾的女孩 2024-12-07 20:47:39

在阅读问题后,您似乎需要在应用程序启动期间保留这些数据(我可能完全错了)。如果这是真的,你应该看看 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 of NSStrings (group IDs corresponding to groups to which it belongs) & each Group class will store an array of NSStrings (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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文