使用弱引用键的Swift字典?

时间:2022-10-02 16:10:04

Let's say I have a some objects representing network connections. Once these connections are disconnected, the associated objects disappear. I don't want to hang on to a connection object which is no longer connected.

假设我有一个表示网络连接的对象。一旦这些连接被断开,相关的对象就会消失。我不想挂在一个不再连接的连接对象上。

I also want to associate some data with these connections using a dictionary. So I might have the code:

我还想使用字典将一些数据与这些连接关联起来。所以我可能有代码:

class Connection { ... }
class Metadata { ... }

var metadata: [Connection: Metadata] = [:]

But the above code means that the dictionary will keep references to the Connection objects which I don't want. I'd prefer to have the associated entries be removed, ideally automatically, when the Connection objects disappear.

但是上面的代码意味着字典将保持对连接对象的引用,而我不希望这样。我希望在连接对象消失时自动删除相关条目。

So I tried:

所以我试着:

var metadata: [weak Connection: Metadata] = [:]

But this doesn't work. What is a good alternative solution to this?

但这是行不通的。有什么好的替代方案吗?

2 个解决方案

#1


13  

You are describing an NSMapTable. It gives you a dictionary-like thing with weak references to its keys and/or values.

您正在描述一个NSMapTable。它为您提供了一个类字典的东西,它的键和/或值的弱引用。

#2


2  

You can write a generic type for weak references like they did in How do I declare an array of weak references in Swift? Because you are doing this for the dictionary key, you have to go through some extra work to make it conform to Hashable, but it can be done.

您可以为弱引用编写通用类型,就像我如何在Swift中声明弱引用数组一样。因为你这样做是为了字典的关键,你必须通过一些额外的工作使它符合可洗,但它是可以做到的。

Personally, though, I wouldn't use the connection objects as the key. I use a unique string identifier for the network request be the key (e.g., the taskIdentifier of the NSURLSessionTask).

但就我个人而言,我不会使用连接对象作为键。我使用唯一的字符串标识符作为网络请求的键(例如,NSURLSessionTask的taskIdentifier)。

That resolves the concern of the collection maintaining a strong reference to the request.

这解决了集合的关注,维护了对请求的强烈引用。

Regarding the removing of the item when the task is done, I just make this clean-up part of the task completion logic.

关于在任务完成时删除项目,我只是将清理工作作为任务完成逻辑的一部分。

#1


13  

You are describing an NSMapTable. It gives you a dictionary-like thing with weak references to its keys and/or values.

您正在描述一个NSMapTable。它为您提供了一个类字典的东西,它的键和/或值的弱引用。

#2


2  

You can write a generic type for weak references like they did in How do I declare an array of weak references in Swift? Because you are doing this for the dictionary key, you have to go through some extra work to make it conform to Hashable, but it can be done.

您可以为弱引用编写通用类型,就像我如何在Swift中声明弱引用数组一样。因为你这样做是为了字典的关键,你必须通过一些额外的工作使它符合可洗,但它是可以做到的。

Personally, though, I wouldn't use the connection objects as the key. I use a unique string identifier for the network request be the key (e.g., the taskIdentifier of the NSURLSessionTask).

但就我个人而言,我不会使用连接对象作为键。我使用唯一的字符串标识符作为网络请求的键(例如,NSURLSessionTask的taskIdentifier)。

That resolves the concern of the collection maintaining a strong reference to the request.

这解决了集合的关注,维护了对请求的强烈引用。

Regarding the removing of the item when the task is done, I just make this clean-up part of the task completion logic.

关于在任务完成时删除项目,我只是将清理工作作为任务完成逻辑的一部分。