Skip to content

Instantly share code, notes, and snippets.

@yannxou
Created June 22, 2022 12:12
Show Gist options
  • Save yannxou/34cec23d005ba6005245eb69f856b9e1 to your computer and use it in GitHub Desktop.
Save yannxou/34cec23d005ba6005245eb69f856b9e1 to your computer and use it in GitHub Desktop.
CancellableTestPlayground #Swift #Combine
// 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