Skip to content

Instantly share code, notes, and snippets.

@taichino
Created May 13, 2022 21:28
Show Gist options
  • Save taichino/479ae77c15bd68ca6fbd871fe4badb30 to your computer and use it in GitHub Desktop.
Save taichino/479ae77c15bd68ca6fbd871fe4badb30 to your computer and use it in GitHub Desktop.
Observing an array test
import Combine
var list = ["A", "B", "C"]
let cancellable = list.publisher
.collect()
.sink { completion in
print("Completed with \(completion)")
} receiveValue: { value in
print(value)
}
print("@@ start mutating list @@")
list.append("D")
list.removeAll { $0 == "B"}
print(cancellable)
// output looks like the following.
// ---
// ["A", "B", "C"]
// Completed with finished
// @@ start mutating list @@
// Combine.AnyCancellable
// ---
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment