Why you should stop using NotificationCenter and start using TopicEventBus

Matan Abravanel
Aug 5, 2018 · 3 min read

What is an EventBus?

Isn’t EventBus the same as NSNotificationCenter?

Show me the code! And tell me what’s in it for me.

Let’s build a chat app!

class ConversationChangedEvent: TopicEvent {
let newTitle: String
init(conversationId: String, newTitle: String) {
self.newTitle = newTitle
super.init()
self.key = conversationId
}
}
class ConversationVC: UIViewController {
let topicEventBus: TopicEventBus
let conversationId = "1234"

init(topicEventBus: TopicEventBus) {
self.topicEventBus = topicEventBus
}

override func viewDidLoad() {
super.viewDidLoad()
_ = self.topicEventBus.subscribe(topic: conversationId, callback: { (conversationChangedEvent: ConversationChangedEvent) in
// This will run every time "ConversationChangedEvent"
// with id 1234 will be fired.
})
}
}
class FiringService {
let topicEventBus: TopicEventBus
init(topicEventBus: TopicEventBus) {
self.topicEventBus = topicEventBus
}

func conversationTitleChanged() {
self.topicEventBus.fire(event: ConversationChangedEvent.init(conversationId: "1234",
newTitle: "First update"))
}
}

You did it! You fired your first event with topic. 🤗 🎉

TopicEventBus

Swift2Go

a place where Swift Developers share knowledge.

Matan Abravanel

Written by

The Man Who Does Not Read Has No Advantage Over the Man Who Cannot Read

Swift2Go

Swift2Go

a place where Swift Developers share knowledge.