Skip to content

edgarclerigo/SwiftEventBus

 
 

Repository files navigation

SwiftEventBus

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

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

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

Unregistering

Remove all the observers from the target

SwiftEventBus.unregister(target)

Remove observers of the same name from the target

SwiftEventBus.unregister(target, "someEventName")

About

A publish/subscribe EventBus optimized for iOS

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Swift 87.4%
  • Ruby 7.1%
  • C++ 5.5%