Skip to content

Instantly share code, notes, and snippets.

@SlappyAUS
Last active November 29, 2020 09:17
Show Gist options
  • Save SlappyAUS/387a4d25a6cf4a158410dc4915214203 to your computer and use it in GitHub Desktop.
Save SlappyAUS/387a4d25a6cf4a158410dc4915214203 to your computer and use it in GitHub Desktop.
Pub/Sub Property Sample #swift #combine
// The list of drinks consumed.
// Because this is @Published property,
// Combine notifies any observers when a change occurs.
@Published public var currentDrinks = [Drink]()
// A sink that is also called whenever the currentDrinks array changes.
var updateSink: AnyCancellable!
// Add a subscriber to currentDrinks that responds whenever currentDrinks changes.
updateSink = $currentDrinks.sink { [unowned self] _ in
// Update any complications on active watch faces.
let server = CLKComplicationServer.sharedInstance()
for complication in server.activeComplications ?? [] {
server.reloadTimeline(for: complication)
}
// Begin saving the data.
self.save()
}
// The deinitializer for the model object.
deinit {
// Cancel the observer.
updateSink.cancel()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment