Use minor in closing the lid for Swift

Do I have a closure inside the closure, and the second closure uses self, so should both have an unoccupied me, or just the second closure should have it?

dispatch_async(backgroundQueue) { [unowned self] () -> Void in
    dispatch_async(dispatch_get_main_queue(), { [unowned self] () -> Void in
        self.doSomething()
    })
}
+4
source share
2 answers

This is a save schedule without unowned, it has no cycles, so you don’t need unownedto break something.

a -> b means save b

backgroundQueue -> outerBlock -> self
                       |          ^
                       V          |
      mainQueue -> innerBlock -----

A cycle is formed only when selfany of the blocks is saved.

Also note that even backgroundQueuesaves outerBlock, the block will be released after execution, so if you save, the save backgroundQueuecycle will not continue.


unowned ( )

a -x- b b, (unowned)

  backgroundQueue -> outerBlock -x- self
                           |          |
                           V          x
          mainQueue -> innerBlock -----

, self , , innerBlock .

+6

?

. dispatch_async, [unowned self] . . .

+1

All Articles