Allows publish-subscribe-style communication between components without requiring the components to explicitly be aware of each other
- simplifies the communication between components
- decouples event senders and receivers
- avoids complex and error-prone dependencies and life cycle issues
- makes your code simpler
- is fast
- is tiny
- Thread-safe
pod 'SwiftEventBus', :tag => '5.1.0', :git => 'https://github.com/cesarferreira/SwiftEventBus.git'github "cesarferreira/SwiftEventBus" == 5.1.05.+forswift 53.+forswift 4.22.+forswift 31.1.0forswift 2.2
Subscribers implement event handling methods that will be called when an event is received.
SwiftEventBus.onMainThread(target, name: "someEventName") { result in
// UI thread
}
// or
SwiftEventBus.onBackgroundThread(target, name:"someEventName") { result in
// API Access
}Post an event from any part of your code. All subscribers matching the event type will receive it.
SwiftEventBus.post("someEventName")--
Post event
SwiftEventBus.post("personFetchEvent", sender: Person(name:"john doe"))Expecting parameters
SwiftEventBus.onMainThread(target, name:"personFetchEvent") { result in
let person : Person = result.object as Person
println(person.name) // will output "john doe"
}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:
@IBAction func clicked(sender: AnyObject) {
count++
SwiftEventBus.post("doStuffOnBackground")
}
@IBOutlet weak var textField: UITextField!
var count = 0
override func viewDidLoad() {
super.viewDidLoad()
SwiftEventBus.onBackgroundThread(self, name: "doStuffOnBackground") { notification in
println("doing stuff in background thread")
SwiftEventBus.postToMainThread("updateText")
}
SwiftEventBus.onMainThread(self, name: "updateText") { notification in
self.textField.text = "\(self.count)"
}
}
//Perhaps on viewDidDisappear depending on your needs
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
SwiftEventBus.unregister(self)
}--
Remove all the observers from the target
SwiftEventBus.unregister(target)Remove observers of the same name from the target
SwiftEventBus.unregister(target, "someEventName")
Formed in 2009, the Archive Team (not to be confused with the archive.org Archive-It Team) is a rogue archivist collective dedicated to saving copies of rapidly dying or deleted websites for the sake of history and digital heritage. The group is 100% composed of volunteers and interested parties, and has expanded into a large amount of related projects for saving online and digital history.
