Git Product home page Git Product logo

eventbroadcaster-ios's Introduction

EventBroadcaster-iOS

EventBroadcaster is a lightweight event handler written in swift for iOS, macOS, tvOS & watchOS applications.

Cocoapods

EventBroadcaster is available through CocoaPods. Simply add the following to your Podfile:

use_frameworks!

target '<Your Target Name>' do
  pod 'EventBroadcaster'
end

How to use

  • Import it to your project:
import EventBroadcaster
  • Declare events: create a file in your project to implement an extension for EventBroadcaster class, to put event names there. (File name can be anything like "Events.swift") In that file add events like this:
extension EventBroadcaster {
    static let DownloadCompleted = 0
    static let ConnectionLost = 1
    static let SettingsUpdated = 2
    static let AnyOtherEvent = 3
}

Make sure to set a unique event ID for each event.

  • Setup observer: An event observer will receive updates from related events. Observer needs to implement EventBroadcasterDelegate methods:
extension ViewController: EventBroadcasterDelegate {
    func didReceivedNotification(_ id: Int, args: [Any]) {
        if id == EventBroadcaster.SettingsUpdated {
            // Cast expected arguments from args elements
            // For example, args[0] as? String or whatever
        } else if id == EventBroadcaster.DownloadCompleted {
            // Do whatever you want
        }
    }
}
  • Add observer: Now your ViewController (or any other class that you need) is ready to observe for events. So add it as observer to events that it needs: (You can do it in init() or wherever you want)
override func viewDidLoad() {
    super.viewDidLoad()
    EventBroadcaster.sharedInstance.addObserver(self, EventBroadcaster.SettingsUpdated)
    EventBroadcaster.sharedInstance.addObserver(self, EventBroadcaster.DownloadCompleted)
}
  • Broadcast event: Anyone from anywhere can broadcast any event; for example your DownloadManager wants to broadcast that a file download completed:
EventBroadcaster.sharedInstance.broadcastEvent(EventBroadcaster.DownloadCompleted, [fileID, fileSize])
  • Remove observer: Since EventBroadcaster holds a weak refrence of observer, you don't need to worry about memory leak but we've got an emergancy method to make sure that the observer is refrence is removed. Also you can use it when you want to stop listening to an event updates.
deinit {
   EventBroadcaster.sharedInstance.removeObserver(self, EventBroadcaster.SettingsUpdated)
   EventBroadcaster.sharedInstance.removeObserver(self, EventBroadcaster.DownloadCompleted)
}

Advanced:

  • Sometimes for any reason, you want your broadcaster to stop broadcasting for a while and keep updates until you want it to flush them; For example I stop broadcasting while my app is performing an animation because I don't want my animation lagging (It is possible to have many events at the moment). EventBroadcaster guarantees to stack your events by their orders when you tell it there is an animation, and flush them all when you tell it the animation is completed:
EventBroadcaster.sharedInstance.setAnimationInProgress(value: true)
// Animation
EventBroadcaster.sharedInstance.setAnimationInProgress(value: false)

eventbroadcaster-ios's People

Contributors

alisamaiee avatar rezpilehvar avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar

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.