Skip to content

Instantly share code, notes, and snippets.

@bjhomer
Last active June 2, 2017 19:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save bjhomer/e866a405c425e83c8cad53a8ee8f055e to your computer and use it in GitHub Desktop.
Save bjhomer/e866a405c425e83c8cad53a8ee8f055e to your computer and use it in GitHub Desktop.
Proof that NSOperation.completionBlock is not being cleared.
// Run this in an iOS playground. It works correctly on Mac.
import Foundation
import PlaygroundSupport
let page = PlaygroundPage.current
page.needsIndefiniteExecution = true
func test() {
let op = Operation()
op.completionBlock = {
print(op.completionBlock) // prints "Optional((Function))"
DispatchQueue.main.asyncAfter(deadline: .now() + 1, execute: {
print(op.completionBlock) // prints "Optional((Function))"
op.completionBlock = nil
print(op.completionBlock) // prints "nil"
page.finishExecution()
})
}
op.start()
}
test()
// On macOS, these all print `nil`, as expected
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment