SwiftEventBus

A publish/subscribe EventBus optimized for iOS

Github stars Tracking Chart

SwiftEventBus

Language
Language
Language
Language

Allows publish-subscribe-style communication between components without requiring the components to explicitly be aware of each other

Features

  • 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

Installation

Cocoapods

pod 'SwiftEventBus', :tag => '5.0.1', :git => 'https://github.com/cesarferreira/SwiftEventBus.git'

Carthage

github "cesarferreira/SwiftEventBus" == 5.0.1

Versions

  • 5.+ for swift 5
  • 3.+ for swift 4.2
  • 2.+ for swift 3
  • 1.1.0 for swift 2.2

Usage

1 - Prepare subscribers

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
}

2 - Post events

Post an event from any part of your code. All subscribers matching the event type will receive it.

SwiftEventBus.post("someEventName")

--

Eventbus with parameters

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"
}

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:


@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)
}

--

Unregistering

Remove all the observers from the target

SwiftEventBus.unregister(target)

Remove observers of the same name from the target

SwiftEventBus.unregister(target, "someEventName")

Overview

Name With Ownercesarferreira/SwiftEventBus
Primary LanguageSwift
Program languageSwift (Language Count: 3)
Platform
License:MIT License
Release Count14
Last Release Name5.1.0 (Posted on 2021-03-17 11:41:58)
First Release Name1.0.0 (Posted on 2015-02-08 23:51:16)
Created At2015-02-06 19:45:05
Pushed At2021-03-17 11:42:00
Last Commit At2021-03-17 11:41:52
Stargazers Count1.1k
Watchers Count34
Fork Count101
Commits Count82
Has Issues Enabled
Issues Count42
Issue Open Count12
Pull Requests Count17
Pull Requests Open Count0
Pull Requests Close Count2
Has Wiki Enabled
Is Archived
Is Fork
Is Locked
Is Mirror
Is Private
To the top