Skip to content

Instantly share code, notes, and snippets.

@gokselkoksal
Last active July 31, 2018 07:41
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gokselkoksal/4ab590f24305e072a547af46d81c056e to your computer and use it in GitHub Desktop.
Save gokselkoksal/4ab590f24305e072a547af46d81c056e to your computer and use it in GitHub Desktop.
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
Copy link

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