Last active
November 29, 2020 09:17
-
-
Save SlappyAUS/387a4d25a6cf4a158410dc4915214203 to your computer and use it in GitHub Desktop.
Pub/Sub Property Sample #swift #combine
This file contains hidden or 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
| // 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