Skip to content

Instantly share code, notes, and snippets.

@gtokman
Last active March 27, 2021 17:03
Show Gist options
  • Save gtokman/c86fab5505b90846240fb6b2ff401f59 to your computer and use it in GitHub Desktop.
Save gtokman/c86fab5505b90846240fb6b2ff401f59 to your computer and use it in GitHub Desktop.
Passthrough subject - combine
// Create passthrough subject
let todoSubject = PassthroughSubject<Todo, Never>()
// Release subscription from memory when done
var cancellable: AnyCancellable?
// Subscribe to updates
cancellable = todoSubject
.sink { todo in
print("New todo to add to table view:", todo.task)
}
// Imagine we had UI for adding a todo
let newTodo = Todo(task: "Groceries")
// Publish new todo
todoSubject.send(newTodo)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment