热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

哈希表中的弱引用。-ARCweakreferencesinhashtable

IveusinganNSHashTabletostoreweakreferencestoobjects.Whenanobjectinthehashtableisd

I've using an NSHashTable to store weak references to objects. When an object in the hash table is deallocated and the reference is zeroed within the hash table, the pointer to nil still resides in the hash table, correct? So over time, if objects that are deallocated aren't directly removed from the hash table, the hash table will continue to grow in size? Right now, there is no easy way to send a notification to the object controlling the hash table that one of the hash tables elements have been removed (it's a game where feedback calls mean communication between objects is very indirect, and preferably stays that way). Should I iterate over the hash table periodically to remove nil references?

我使用NSHashTable来存储对象的弱引用。当哈希表中的一个对象被释放并且引用在哈希表中归零时,指向nil的指针仍然驻留在哈希表中,对吗?所以随着时间的推移,如果释放的对象没有直接从哈希表中删除,哈希表的大小会继续增加?现在,没有一种简单的方法可以向控制哈希表的对象发送一个通知,该对象已经删除了哈希表的一个元素(在这个游戏中,反馈调用意味着对象之间的通信非常间接,最好保持这种方式)。我应该定期遍历哈希表以删除nil引用吗?

2 个解决方案

#1


7  

This question was posted several months ago, but for the record, NSHashTable does remove released weak references. So to answer the actual question, no, you don't need to iterate over the hash table periodically.

这个问题是几个月前发布的,但是对于这个记录,NSHashTable确实删除了弱引用。要回答实际的问题,不,你不需要周期性地遍历哈希表。

#2


1  

I would make a manager class which is responsible for adding and removing objects you are speaking of. Thus I would notify the hash table in one of several ways:

我将创建一个manager类,它负责添加和删除您所说的对象。因此,我将以以下几种方式之一通知哈希表:

  • directly via method invocation (when hash table is referenced by manager)
  • 直接通过方法调用(当管理器引用哈希表时)
  • indirectly making hash table a manager's delegate object
  • 间接地使哈希表成为管理器的委托对象
  • indirectly registering hashtable to observe any add/remove operations in manager
  • 间接注册hashtable以观察manager中的任何添加/删除操作
  • indirectly by making manager sending a notification to notification center, which notification is listened by the hash table
  • 通过使manager向通知中心发送通知,该通知由散列表侦听

It all depends how did you bind together those objects and which models suits the most actual design.

这完全取决于如何将这些对象绑定在一起,以及哪种模型适合最实际的设计。


推荐阅读
author-avatar
雨的到来2009
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有