Skip to content

Instantly share code, notes, and snippets.

@csknns
Created July 18, 2021 16:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save csknns/225fd12332e349f55664effe90938f51 to your computer and use it in GitHub Desktop.
Save csknns/225fd12332e349f55664effe90938f51 to your computer and use it in GitHub Desktop.
Example of Future that will crash when it resolves, if handleEvents capture's self and self is de-allocated
import Foundation
import Combine
var dispatchGroup = DispatchGroup()
dispatchGroup.enter()
DispatchQueue.global().asyncAfter(deadline: .now() + 3, execute: {
dispatchGroup.leave()
})
class AClass {
var subscriptions: Set<AnyCancellable> = []
init() {
Future<Void, Never> { resolve in
DispatchQueue.global().asyncAfter(deadline: .now() + 2, execute: {
print("resolving")
resolve(.success(()))
print("resolved")
})
}
.handleEvents(receiveCancel: {
//I need to perform cleanup work here if the self is deallocated
print("received cancel \(self)")
self.aFunction()
})
.sink { _ in
}.store(in: &subscriptions)
}
deinit {
print("AClass deinit")
}
func aFunction() {}
}
var aClass: AClass? = AClass()
aClass = nil
dispatchGroup.wait()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment