TopicEventBus
Publish–subscribe design pattern implementation framework, with ability to publish events by topic. (NotificationCenter extended alternative)
TopicEventBus is Easy to use, type safe way of implementing Publish–subscribe design pattern.
There are many other libraries out there, aiming to solve this issue, but none of them support publishing events by topic, in a type-safe way, with no magic strings.
Also, TopicEventBus holds weak referenced for Its observers, so you don't have to be afraid of memory leaks.
What is a topic?
The topic is for example, when you would like to publish "ConversationUpdateEvent" yet have the ability to publish that event only to listeners with conversation id "1234" or to all listeners.
Specifying the conversation Id is specifying a topic for that event.
Show me the code! and what's in it for me.
Let's build a chat app!
In this app, we are going to have multiple conversations screens, each one of them would like to know only about changes to Its conversation.
The first step, create an event for conversation update:
Every event must inherit from "TopicEvent," and in case it has a topic (Our example it will be the conversation id) set "key" property to the correct value.
Now inside ConversationVC, subscribe to this event:
Notice you only need to specify the return value you are expecting, for TopicEventBus to figure out the event you are subscribing for
This is how you fire an event:
You did it! You fired your first event with topic ? ?
API
If you subscribe to event and specify no topic, you will receive all events from that type, no matter their key:
Unsubscribe by calling "stop" on the returned object after subscribing:
On app log out, terminate TopicEventBus by just calling:
Example project
Please notice example project contains tests to support all edge cases, plus example code from the above snippets.
Installation
Cocoapods
TopicEventBus is available through CocoaPods. To install
it, simply add the following line to your Podfile:
Manually
- Download and drop
/TopicEventBus
folder in your project. - Congratulations!