A publish/subscribe EventBus optimized for iOS

Overview

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.1.0', :git => 'https://github.com/cesarferreira/SwiftEventBus.git'

Carthage

github "cesarferreira/SwiftEventBus" == 5.1.0

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")
Comments
  • Added @discardableResult

    Added @discardableResult

    Adding @discardableResult notation will remove the following warning:

    Result of call to 'onMainThread(_:name:handler:)' is unused

    And other similar.

    opened by shshalom 9
  • onMainThread not fired

    onMainThread not fired

    Hi

    I use "SwiftEventBus.post" when user clicks on the notification and open the app. but in another side "onMainThread" not fired.

    is there an issue or I miss up some thing.

    can you help me?

    Thanks

    opened by malkabani 7
  • SwiftEventBus 2.0.0 only available for ios 10

    SwiftEventBus 2.0.0 only available for ios 10

    when I use cocoapods ,set ios plaform 9.0, then the log:Specs satisfying the SwiftEventBus (fromhttps://github.com/cesarferreira/SwiftEventBus.git, tag2.0.1), SwiftEventBus (= 2.0.0) dependency were found, but they required a higher minimum deployment target. I try to set plaform 10.0 ,it's ok

    opened by owen19891030 5
  • [Cococapods][SwiftEventBus 2.0.0] A dependency with an external source may not specify version requirements

    [Cococapods][SwiftEventBus 2.0.0] A dependency with an external source may not specify version requirements

    I want to install SwiftEventBus 2.0.0 by cocoapods, So I input Podfile below and execute pod install.

    pod 'SwiftEventBus', '~> 2.0.0', :git => 'https://github.com/cesarferreira/SwiftEventBus.git'
    

    But error is occured.

    [!] A dependency with an external source may not specify version requirements (SwiftEventBus).
    

    please help me.

    opened by imagepit 5
  • Carthage support

    Carthage support

    @heckfer and I added Carthage support to SwiftEventBus, we just had to share the scheme. We also had to create a new tag, to make it work you need to create a new tag manually in your project repository as well.

    Cheers

    opened by migore 5
  • Module compiled with Swift 5.1.3 cannot be imported by the Swift 5.1 compiler:

    Module compiled with Swift 5.1.3 cannot be imported by the Swift 5.1 compiler:

    I have installed SwiftEventBus in my project using Carthage and it shows me this error while running my project. I am using Xcode 11.1, also check this in 11.3. Can someone please help how I can resolve this ?

    opened by ioskunal 3
  • Sometime event seems to be sent multiple times.

    Sometime event seems to be sent multiple times.

    Even if I only send it once, my registered closure is called multiple times. So today i have to unregister when receiving the event for the first time.

    Note that I'm certain that the posting of the event happens only once, I added a breakpoint and it only stopped once.

    Sending: SwiftEventBus.post(BUS_EVENT_1_DETECTED, sender: myData)

    Event listener in my view controller viewDidLoad() function :

        SwiftEventBus.onBackgroundThread(self, name:BUS_EVENT_1_DETECTED) { result in
            //listen only once as this event may be casted multiple times.
            SwiftEventBus.unregister(self, name:BUS_EVENT_1_DETECTED)
            let data:MyData = result.object as! MyData
            // then do stuff here with my data.
        } 
    

    Any ideas ? Thanks.

    opened by nmDejamobile 3
  • issue with updating UI with a SwiftEventBus.post fired on a background thread

    issue with updating UI with a SwiftEventBus.post fired on a background thread

    I'm having an issue with updating UI with a SwiftEventBus.post fired on a background thread. The issue happens when i'm making a network request for authenticating a user.

    We have an event for when the user is authenticated. When the user is authenticated, we fire off a SwiftEventBus event(which looks to be done on a background thread).

    The completion handler for the subscriber to the authentication event also publishes an event to notify the UI to update, but the event never gets caught by it's subscriber. The view controller that subscribes to the "navigate" event set's the subscription handler's "onMainThread" to true, but method never seems to get called.

    Am I supposed to ensure that events relate to updating UI need to be fired on the main thread, or is this a known issue?

    opened by ChristopherRyanCruz 3
  • Swift 1.2 Support

    Swift 1.2 Support

    Seeing build errors when compiling with XCode 6.3 beta 3 / Swift 1.2:

    /Users/ragrait/ios/Rdio/Pods/SwiftEventBus/SwiftEventBus/SwiftEventBus.swift:48:43: Invalid use of '()' to call a value of non-function type 'UInt'

    This is happening in 3 different places, but they're all variants on this:

    let id = ObjectIdentifier(target).uintValue()
    

    Looks like the trailing parens cause a syntax error in Swift 1.2

    opened by raulagrait 3
  • Conflicting overloading functions

    Conflicting overloading functions

    These two functions conflicting

    open class func post(_ name: String, sender: AnyObject?)
    open class func post(_ name: String, sender: NSObject?)
    

    Swift 4.2 by calling

    SwiftEventBus.post("notification", sender: 1) // not working
    SwiftEventBus.post("notification", sender: 1 as AnyObject) // working
    
    opened by chanonly123 2
  • Add missing shared scheme for carthage

    Add missing shared scheme for carthage

    I run carthage update and I get "Project has no shared schemes" error. I think you accidentally deleted it by e4e8d93 so I share it again. Please have a look.

    opened by ykn-mst 2
  • Module compiled with Swift 5.3.2 cannot be imported by the Swift 5.4.2 compiler

    Module compiled with Swift 5.3.2 cannot be imported by the Swift 5.4.2 compiler

    I have installed SwiftEventBus in my project using Carthage and it shows me this error while running my project. I used Xcode 12.5.1 and 13 beta.

    • Xcode 12.5.1: Module compiled with Swift 5.3.2 cannot be imported by the Swift 5.4.2 compiler
    • Xcode 13 beta: Module compiled with Swift 5.3.2 cannot be imported by the Swift 5.5 compiler

    Can someone please help how I can resolve this?

    opened by bbiguduk 2
  • Cocoapods downloads 5.0.0 instead of the latest

    Cocoapods downloads 5.0.0 instead of the latest

    Hi.

    I'm on Xcode 12 + Swift 5 Adding this line to my Podfile

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

    downloads 5.0.0 instead of 5.1.0.

    Update all pods
    Updating local specs repositories
    Analyzing dependencies
    Pre-downloading: `SwiftEventBus` from `https://github.com/cesarferreira/SwiftEventBus.git`, tag `5.1.0`
    Downloading dependencies
    Installing SwiftEventBus 5.0.0
    

    I see the Podspec is set to 5.0.0. Would that be the reason it pulls 5.0.0? Sorry I'm new to iOS development.

    opened by kmspop 0
Releases(5.1.0)
  • 5.1.0(Mar 17, 2021)

    • a30ff35 bumped to 5.1.0
    • 4b4fbcf Merge pull request #54 from driivz/master
    • 8fdb6e8 Merge branch 'master' into master
    • 3d0540f Update project for SPM 5.3
    • c904a84 Merge pull request #51 from robertogvieira/fix/Xcode_12_warnings
    • 1ab88a1 Fix Xcode 12 warnings.

    https://github.com/cesarferreira/SwiftEventBus/compare/5.0.1...5.1.0

    Source code(tar.gz)
    Source code(zip)
  • 5.0.1(Dec 4, 2019)

    • e34a15d Swift Package (#47)
    • b0cce49 updated swift package
    • 6c6191e added swift package updated example to 5.0
    • 7cc44cd Update README.md
    • 5f214c8 Merge pull request #46 from satishVekariya/master
    • a2b1d3d Update README.md

    https://github.com/cesarferreira/SwiftEventBus/compare/5.0.0...5.0.1

    Source code(tar.gz)
    Source code(zip)
Owner
César Ferreira
Senior Android Developer, currently working as a Tech Lead @GlueHome
César Ferreira
A Swift based Future/Promises Library for IOS and OS X.

FutureKit for Swift A Swift based Future/Promises Library for IOS and OS X. Note - The latest FutureKit is works 3.0 For Swift 2.x compatibility use v

null 759 Dec 2, 2022
Lightweight promises for iOS, macOS, tvOS, watchOS, and Linux

Futures Futures is a cross-platform framework for simplifying asynchronous programming, written in Swift. It's lightweight, fast, and easy to understa

David Ask 60 Aug 11, 2022
Material para a apresentação da palestra "Implementando Interesses Transversais - um papo sobre arquitetura, DI e Design Patterns em Swift/iOS" no TDC Future 2021

--- title: Implementando Interesses Transversais - um papo sobre arquitetura, DI e Design Patterns em Swift/iOS author: Cícero Camargo date: Nov 30th

Cícero Camargo 2 Nov 30, 2021
A dead-simple abstraction over the iOS BackgroundTask API to make background tasks easy to isolate, maintain and schedule

A dead-simple abstraction over the iOS BackgroundTask API to make background tasks easy to isolate, maintain and schedule. Designed to be as lightweight and flexible as possible while tightly integrating with the system APIs. And It's built with Swift Concurrency in mind.

Sam Spencer 31 Dec 11, 2022
A publish/subscribe EventBus optimized for iOS

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

César Ferreira 1k Dec 15, 2022
Publish–subscribe design pattern implementation framework, with an ability to publish events by topic.

TopicEventBus Publish–subscribe design pattern implementation framework, with ability to publish events by topic. (NotificationCenter extended alterna

Matan Abravanel 55 Nov 29, 2021
Publish–subscribe design pattern implementation framework, with an ability to publish events by topic.

TopicEventBus Publish–subscribe design pattern implementation framework, with ability to publish events by topic. (NotificationCenter extended alterna

Matan Abravanel 55 Nov 29, 2021
TopicEventBus is Easy to use, type safe way of implementing Publish–subscribe design pattern.

TopicEventBus Publish–subscribe design pattern implementation framework, with ability to publish events by topic. (NotificationCenter extended alterna

Matan Abravanel 55 Nov 29, 2021
📬 A lightweight implementation of an observable sequence that you can subscribe to.

Features Lightweight Observable is a simple implementation of an observable sequence that you can subscribe to. The framework is designed to be minima

Felix M. 133 Aug 17, 2022
📬 A lightweight implementation of an observable sequence that you can subscribe to.

Features Lightweight Observable is a simple implementation of an observable sequence that you can subscribe to. The framework is designed to be minima

Felix M. 133 Aug 17, 2022
📝 The next in the generations of 'growing textviews' optimized for iOS 8 and above.

NextGrowingTextView The next in the generations of 'growing textviews' optimized for iOS 8 and above. As a successor to HPGrowingTextView, NextGrowing

Muukii 1.7k Jan 7, 2023
Dogtector: dog breed detection app for iOS using YOLOv5 model combined with Metal based object decoder optimized

Dogtector Project description Dogtector is dog breed detection app for iOS using YOLOv5 model combined with Metal based object decoder optimized for u

Bartłomiej Pluta 21 Aug 1, 2022
📝 The next in the generations of 'growing textviews' optimized for iOS 8 and above.

NextGrowingTextView The next in the generations of 'growing textviews' optimized for iOS 8 and above. As a successor to HPGrowingTextView, NextGrowing

Muukii 1.7k Dec 25, 2022
FlexLayout adds a nice Swift interface to the highly optimized facebook/yoga flexbox implementation. Concise, intuitive & chainable syntax.

FlexLayout adds a nice Swift interface to the highly optimized Yoga flexbox implementation. Concise, intuitive & chainable syntax. Flexbox is an incre

layoutBox 1.7k Dec 30, 2022
Work in progress gallery of controls available to Catalyst apps using Optimized for Mac

Catalyst Controls Gallery Very simple work-in-progress demonstration of many common controls available to Mac Catalyst as of macOS 11. Provided moreso

Steven Troughton-Smith 163 Sep 18, 2022
Mappable - Flexible JSON to Model converter, specially optimized for immutable properties

Mappable is a lightweight, flexible, easy-to-use framework to convert JSON to model, specially optimized for immutable property initializatio

Leave 27 Aug 26, 2022
A (really) native and powerful macOS Telegram client built using SwiftUI, optimized for moderating large communities and personal use.

Moc A (really) native and powerful macOS Telegram client, optimized for moderating large communities and personal use. This client is currently in dev

GGorAA 84 Jan 2, 2023
Publish and discover services using Bonjour

Ciao Lib to publish and find services using mDNS Requirements Installation Usage License Requirements iOS 8.0+ / Mac OS X 10.10+ / tvOS 9.0+ Xcode 9.0

Alexandre Mantovani Tavares 55 Dec 14, 2022
Swift-DocC is a documentation compiler for Swift frameworks and packages aimed at making it easy to write and publish great developer documentation.

Swift-DocC is a documentation compiler for Swift frameworks and packages aimed at making it easy to write and publish great developer docum

Apple 833 Jan 3, 2023
SocialButtons - A plugin for Publish that allows you to easily embed social buttons (e.g. Tweet button) in your site

SocialButtons A plugin for Publish that allows you to easily embed social button

Alan Paiva 6 Dec 1, 2022