Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Using Channels #3
enum Theme: String {
case light, dark
}
class UserSettings {
enum Message {
case didUpdateTheme(Theme)
}
static let shared = UserSettings()
let channel = Channel<Message>()
var theme: Theme = .light {
didSet {
channel.broadcast(.didUpdateTheme(theme))
}
}
init() { }
}
class HomeViewController: UIViewController {
let userSettings = UserSettings.shared
override func viewDidLoad() {
super.viewDidLoad()
subscribe()
}
func subscribe() {
userSettings.channel.subscribe(self) { (message) in
switch message {
case .didUpdateTheme(let theme):
// Apply theme here.
}
}
}
}
@theronic

This comment has been minimized.

Copy link

commented Jul 31, 2018

Hi! Thanks for this.
Can you show how to unsubscribe properly? And how to use the self argument passed into subscribe?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.