I have a weak var WindowController which will close outside a DispatchQueue but no way inside, calling main synchronously or async ?
Replies
Can you show how you call close() ?
Did you try to unown in dispatch closure ?
WindowController.close()
Works fine but nothing within a Queue.
What sort of queue? As always, you need to do it in the main queue.
I always use ...
DispatchQueue.global(qos:?).async {...}
Tried both ...
DispatchQueue.main.async {...}
DispatchQueue.main.sync {...}
`DispatchQueue.global(qos:?).async {...}` cannot be an option. If you always use it and your app is working, it's sort of a miracle.
You usually use `DispatchQueue.main.async {...}` and when that does not work, you may have some flaws in other parts of your code.
If it's as bad as you say why does it exist ?
It does exit for other use cases, not for updating UI.
How can I unown ?
Here is the syntax in a closure:
lazy var someClosure: (Int, String) -> String = {
[unowned self, weak delegate = self.delegate!] (index: Int, stringToProcess: String) -> String in
// closure body goes here
}
Extrait de: Apple Inc. « The Swift Programming Language (Swift 4). » Apple Books.
May read this as well for more detailed illustration
h ttps://www.agnosticdev.com/blog-entry/swift/swift-weak-and-unowned-references
But re-reading this thread, I think OOPer answer explains why it does not work.