Created
July 18, 2021 16:28
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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