SwiftEventBus
Allows publish-subscribe-style communication between components without requiring the components to explicitly be aware of each other
Features
- [x] simplifies the communication between components
- [x] decouples event senders and receivers
- [x] avoids complex and error-prone dependencies and life cycle issues
- [x] makes your code simpler
- [x] is fast
- [x] is tiny
- [x] Thread-safe
Installation
Cocoapods
Carthage
Versions
5.+
forswift 5
3.+
forswift 4.2
2.+
forswift 3
1.1.0
forswift 2.2
Usage
1 - Prepare subscribers
Subscribers implement event handling methods that will be called when an event is received.
2 - Post events
Post an event from any part of your code. All subscribers matching the event type will receive it.
--
Eventbus with parameters
Post event
Expecting parameters
Posting events from the BackgroundThread to the MainThread
Quoting the official Apple documentation:
Regular notification centers deliver notifications on the thread in which the notification was posted
Regarding this limitation, @nunogoncalves implemented the feature and provided a working example:
--
Unregistering
Remove all the observers from the target
Remove observers of the same name from the target