简体   繁体   English

可选关闭是否总是 escaping? 我们应该在上面使用[weak self]还是[unowned self]?

[英]Is optional closure always escaping? Should we use [weak self] or [unowned self] on it?

Give the following function signature in UICollectionView .UICollectionView中给出以下 function 签名。

open func performBatchUpdates(_ updates: (() -> Void)?, completion: ((Bool) -> Void)? = nil)

There are some options over the Internet, that optional closures are always escaping. Internet 上有一些选项,可选闭包始终为 escaping。 But, I am really not sure is that true, or this is still debatable.但是,我真的不确定这是真的,或者这仍然值得商榷。

  1. https://forums.swift.org/t/allowing-escaping-for-optional-closures-in-method-signature/27556 https://forums.swift.org/t/allowing-escaping-for-optional-closures-in-method-signature/27556
  2. https://gist.github.com/gringoireDM/1f18f3bb4e74e2d914a748d89486db56 https://gist.github.com/gringoireDM/1f18f3bb4e74e2d914a748d89486db56

For the above case, do we ever need [weak self] , or [unowned self] in the 1st block, and 2nd black?对于上述情况,我们是否需要[weak self][unowned self]在第一个区块和第二个黑色?

func controllerDidChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>) {
    collectionView!.performBatchUpdates({ () -> Void in
        for operation: BlockOperation in self.blockOperations {
            operation.start()
        }
    }, completion: { (finished) -> Void in
        self.blockOperations.removeAll(keepingCapacity: false)

        self.collectionView.reloadData()
    })
}

According to the compiler (which has the only "opinion" that matters for something like this), an optional closure parameter is always implicitly @escaping .根据编译器(它有唯一对这种事情重要的“意见”),一个可选的闭包参数总是隐含@escaping Test this yourself by writing a function that takes an optional closure and mark it as @escaping .通过编写一个采用可选闭包并将其标记为@escaping的 function 自己进行测试。 The compiler will inform you that it's already escaping.编译器会通知你它已经是 escaping。

As to whether you should capture self as weak or unowned , that depends.至于您是否应该将self捕获为weakunowned ,这取决于。 First I'd stay away from capturing self as unowned , unless you want your program to crash if you're wrong about the lifetime of self - and you might.首先,我会远离将self捕获为unowned ,除非您希望您的程序在您对self的生命周期有误时崩溃 - 您可能会这样做。 I'd rather my program crash than silently do the wrong thing.我宁愿我的程序崩溃也不愿默默地做错事。

But that leaves whether to capture as weak .但这留给是否被俘虏一样weak My opinion is yes, if you're in any doubt capturing a strong reference creating a reference cycle that would prevent it from properly deinitializing.我的意见是肯定的,如果您对捕获强引用有任何疑问,则会创建一个阻止其正确取消初始化的引用循环。 But I wouldn't go so far as to say always or never do something.但我不会 go 说总是从不做某事。 If you are able to reason about your object's life time so that you know the closure will be run and disposed of before the object should normally be deinitialized, then just capture as a strong reference.如果您能够推断出对象的生命周期,以便知道闭包将在 object 通常被取消初始化之前运行和处理,那么只需将其捕获为强参考。 Also capture by strong reference if you purposefully want to extend the life of your object for the sake of the closure and you know that the closure will be executed and disposed of.如果您为了关闭而有意延长 object 的使用寿命,并且您知道关闭将被执行和处理,也可以通过强引用捕获。

Also there is the case where you know that despite being implicitly @escaping that it doesn't actually escape.还有一种情况是你知道尽管隐含地@escaping它实际上并没有逃脱。 Usually that's for a function defined in your code base so you can look at the implementation, but really any time the closure is just a customization point for work that has to be done before the function you're calling returns, you can infer that it doesn't actually escape.通常这是针对在您的代码库中定义的 function 以便您可以查看实现,但实际上任何时候闭包只是在您调用返回的 function 之前必须完成的工作的自定义点,您可以推断它实际上并没有逃脱。 That's not the case for completion handlers, but it might be for other things.完成处理程序不是这种情况,但它可能适用于其他事情。

That's my opinion.这是我的意见。 I like to think it's an informed one, but others might disagree.我喜欢认为这是一个知情的,但其他人可能不同意。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在此关闭过程中,我需要[无主的自我]还是[弱的自我]? - Do I need [unowned self] or [weak self] in this closure? 如果“ self”是ViewController,您是否应该几乎总是使用[weak self]? - If “self” is the ViewController, should you almost always use [weak self]? 弱自我在异步关闭Alamofire中始终为零 - weak self always nil in asynchronous closure Alamofire 这是在闭包中使用弱自我的正确位置吗? - Is this the correct place to use weak self in closure? 当转义的闭包内的self弱时,在self函数中调用self参数 - Invoking a self parameter in a self function when self is weak inside a escaping closure 我应该为Realm.write()使用[unown self]吗? - Should l I use [unowned self] for Realm.write()? 为什么有弱无力的人? 为什么我们不总是使用弱项? - Why are there weak _and_ unowned? Why can we just not always use weak? 我应该在变量中使用弱自我吗? - Should I use weak self in variable? 什么时候应该在闭包中使用捕获列表,什么时候应该使用弱无主vs.无主? - When should i use capture list in closure, and when to use weak vs. unowned? 使用闭包代替UIBarButtonItem BUT的选择器参数,而无需使用弱self - Use closure instead selector argument for UIBarButtonItem BUT without using weak self
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM