问题描述
有什么方法可以检查unowned(safe)
Swift引用的可用性"吗?因此,在此示例中,我正在寻找一个像isReferenceAccessible
这样的假设函数:
Is there any way to check an unowned(safe)
Swift reference for "availability"? So, I am looking for a hypothetical function like isReferenceAccessible
in this example:
func someMethod() {
someAsyncOperation(parameters) { [unowned(safe) self] in
guard isReferenceAccessible(self) else {
return
}
self.someAnotherMethod()
}
}
免责声明:该问题与weak
引用无关!我知道strong
,unowned
和weak
引用是如何工作的.而且我不想使用weak
引用(因为它可能很慢且易变).我知道在尝试访问unowned(safe)
引用时,即使它已经是deinited
,它仍然被分配.而且我知道编译器可以执行此检查,并且实际上可以在应用程序崩溃之前进行检查.
Disclaimer: This question not about weak
references! I am aware of how strong
, unowned
and weak
references work. And I don't want to use weak
references (because it can be slow, and mutable). I know that unowned(safe)
references still be allocated even if it is already deinited
when we are trying to access it. And I know that a compiler can do this check and it actually check it before an application is crashed.
因此,我相信它对于打破现代Swift中的参考周期可能是非常强大且性能良好的技术/范例.
So, I believe it can be very powerful and well-performed technic/paradigm for breaking reference cycles in modern Swift.
此外,我相信这可能是一个很棒的语言功能!例如,假设我们有一个名为shared_ownership
的修饰符,它可以像上面描述的行为一样工作:
Moreover, I believe it can be an awesome language feature! For instance, let's assume that we have the modifier called shared_ownership
and it works thought above described behaviour like this:
method(parameters) { [shared_ownership self] in
self.someAnotherMethod()
}
...具有这样的实现:
... with implementation like this:
method(parameters) { [unowned(safe) self] in
guard isReferenceAccessible(self) else {
return
}
self.someAnotherMethod()
}
...的副作用(没有与weak
相关的复杂性和性能损失)等效于:
... with side effects (without weak
-related complexity and perfomance penalty) equivalent to:
method(parameters) { [weak self] in
guard let strongSelf = self else {
return
}
strongSelf.someAnotherMethod()
}
哦,那太棒了!
有关差异的更多信息在weak
,unowned(safe)
和unowned(unsafe)
之间.
More info about the differences between weak
, unowned(safe)
, and unowned(unsafe)
.
我发现了一个很棒的Swift提议,它与上面讨论的功能有关:允许使用可选绑定将自我从弱引用升级为强引用.
I have found awesome Swift proposal that related the feature discussed above: Allow using optional binding to upgrade self from a weak to strong reference.
推荐答案
突然,我发现我原来的基本假设是Swift中weak
引用可能很慢,这是错误的.正如我们从资源中看到的那样,Swift是实际上对于weak
和unowned
引用使用几乎相同的实现.因此,weak
引用几乎与unowned
引用一样快.
Suddenly I have found that my original base assumption that weak
reference in Swift can be slow is wrong. As we can see from sources, Swift is actually using almost same implementation for weak
and unowned
references. So weak
reference is almost as fast as unowned
reference.
(但是Objective-C的情况完全不同,它使用边表跟踪所有指向周参考的指针,并将deinit,deallocate和清零作为一步.并且它可能很慢.)
(But Objective-C is completely different story, it is using sidetable with tracking of all pointers to week references and deinit, deallocate, and zeroing as one step. And it can be slow.)
因此,我的问题毫无道理.我必须使用周参考,并按照我在原始问题的最后一部分代码中的建议将其拆开.
And because of this, my questions makes no sense. I have to use week reference and unwrap it as I proposed it in the last piece of code in my original question.
更新:这是真棒令人难以置信的Mike Ash撰写的文章,描述了weak
和unowned
引用在Swift中是如何工作的.
Update: Here is an awesome article by incredible Mike Ash that describes how weak
and unowned
references work under the hood in Swift.
这篇关于有没有办法检查已取消初始化“无主"(实际上是“无主(安全)")引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!