Swift NSBlockOperation() Leak: cannot make NSBlockOperation() weak

大城市里の小女人 提交于 2019-12-13 19:47:43

问题


To avoid a memory leak when using NSBlockOperation in Objective-C, we would have to declare the variable as weak to be able to reference the block operation inside the block (to cancel if needed), typically like this:

__weak NSBlockOperation *blockOp  = [NSBlockOperation blockOperationWithBlock:^{
     if (blockOp.cancelled) {
         ...
     }
}];

But in Swift, when I try declare my NSBlockOpeartion as weak, it is always nil.

weak var blockOp = NSBlockOperation()

Without the weak reference, all is fine except it is leaking a little bit of memory each time. How can I reference the block inside the block without leaking memory in Swift?


回答1:


You can use an explicit capture list to capture an unowned reference to the operation. (This is one of the only times I'd actually suggest using unowned references, since the operation will be retained as long as its block is executing. If you're still uncomfortable with that guarantee, you could use weak instead.)

let op = NSBlockOperation()
op.addExecutionBlock { [unowned op] in
    print("hi")
    if op.cancelled { ... }
}

Note that this has to be split into two lines, because the variable can't be referenced from its own initial value.



来源:https://stackoverflow.com/questions/36730768/swift-nsblockoperation-leak-cannot-make-nsblockoperation-weak

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!