Coder Social home page Coder Social logo

cesarferreira / swifteventbus Goto Github PK

View Code? Open in Web Editor NEW
1.1K 34.0 101.0 155 KB

A publish/subscribe EventBus optimized for iOS

License: MIT License

Swift 81.85% Ruby 9.60% Objective-C 8.55%
pub-sub eventbus communication notifications thread ios

swifteventbus's Introduction

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.