Created
June 22, 2022 12:12
-
-
Save yannxou/34cec23d005ba6005245eb69f856b9e1 to your computer and use it in GitHub Desktop.
CancellableTestPlayground #Swift #Combine
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
// Playground to showcase AnyCancellable instances are not automatically released upon completion if they're stored with the `store` function | |
import Combine | |
class LongLivedObject { | |
private(set) var bag = Set<AnyCancellable>() | |
func callCombineOperation() { | |
[1,2,3].publisher.sink { | |
print("completed: \($0)") | |
} receiveValue: { | |
print("value: \($0)") | |
} | |
.store(in: &bag) | |
} | |
} | |
let o = LongLivedObject() | |
o.callCombineOperation() | |
o.bag.count | |
o.callCombineOperation() | |
o.bag.count | |
o.callCombineOperation() | |
o.bag.count |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment