0

1) I cannot understand why is self not required in first closure. Usually you get a compile-time error that goes something like this: "add self to make capture semantics explicit". myCollectionView is an IBOutlet and myItems is a property, they are both strongly reference by self.

2) Is it correct to write [unowned self] in second closure? Am I right to assume that because myCollectionView is referenced strongly by self, using self in its method's completion closure would create a retain cycle is I don't write [weak self]/[unowned self]? The second closure DOES require referencing self explicitly unlike the first one.

guard let index = myItems.index(of: someItem) else { return }
myCollectionView.performBatchUpdates({
    myCollectionView.deleteItems(at: [IndexPath(item: index, section: 0)])
    myItems.remove(at: index)
}, completion: {[unowned self] _ in
    self.doSomething()
})
realvadim
  • 154
  • 12
  • The method is synchronous and the closure parameter not escaping. Compare https://stackoverflow.com/questions/56443959/swift-closures-in-autorelase-pool-accessing-methods-without-self – Martin R Jun 07 '19 at 04:07
  • self only required if closure is `@escaping`. In `performBatchUpdates` method you don't need self for both closures since both are not `@escaping` – RJE Jun 07 '19 at 04:14
  • @RJE the `completion` closure does require `self` – realvadim Jun 07 '19 at 04:17
  • yeah, you are right only first one is synchronous – RJE Jun 07 '19 at 04:26
  • @realvadim see my similar question https://stackoverflow.com/questions/56443959/swift-closures-in-autorelase-pool-accessing-methods-without-self – Prashant Tukadiya Jun 07 '19 at 04:34
  • The second closure is optional and therefore “implicitly escaping,” compare https://stackoverflow.com/a/39619298/1187415. – Martin R Jun 07 '19 at 05:27

0 Answers0