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

Overview

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 '
   
    '
    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()
    do {
        EventBroadcaster.sharedInstance.addObserver(self, EventBroadcaster.SettingsUpdated)
        EventBroadcaster.sharedInstance.addObserver(self, EventBroadcaster.DownloadCompleted)
    } catch {
        print(error)
    }
}
  • 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 {
   try? EventBroadcaster.sharedInstance.removeObserver(self, EventBroadcaster.SettingsUpdated)
   try? 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)
You might also like...
Open source implementation of Apple's Combine framework for processing values over time.
Open source implementation of Apple's Combine framework for processing values over time.

OpenCombine Open-source implementation of Apple's Combine framework for processing values over time. The main goal of this project is to provide a com

SwiftUI-compatible framework for building browser apps with WebAssembly and native apps for other platforms
SwiftUI-compatible framework for building browser apps with WebAssembly and native apps for other platforms

SwiftUI-compatible framework for building browser apps with WebAssembly At the moment Tokamak implements a very basic subset of SwiftUI. Its DOM rende

SwiftUI-compatible framework for building browser apps with WebAssembly and native apps for other platforms
SwiftUI-compatible framework for building browser apps with WebAssembly and native apps for other platforms

SwiftUI-compatible framework for building browser apps with WebAssembly At the moment Tokamak implements a very basic subset of SwiftUI. Its DOM rende

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

A New, Modern Reactive State Management Library for Swift and SwiftUI (The iOS implementation of Recoil)
A New, Modern Reactive State Management Library for Swift and SwiftUI (The iOS implementation of Recoil)

RecoilSwift RecoilSwift is a lightweight & reactive swift state management library. RecoilSwift is a SwiftUI implementation of recoil.js which powered

SwiftEventBus - A publish/subscribe EventBus optimized for iOS

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

Tempura - A holistic approach to iOS development, inspired by Redux and MVVM
Tempura - A holistic approach to iOS development, inspired by Redux and MVVM

Tempura is a holistic approach to iOS development, it borrows concepts from Redux (through Katana) and MVVM. 🎯 Installation Requirements CocoaPods 🤔

Very simple Observable and Publisher implementation for iOS apps.

Very simple Observable and Publisher implementation for iOS apps.

Interactive notification pop-over (aka
Interactive notification pop-over (aka "Toast) modeled after the iOS AirPods and Apple Pencil indicator.

Interactive notification pop-over (aka "Toast) modeled after the iOS AirPods and Apple Pencil indicator. Installation The recommended way is to use Co

Owner
Ali Samaiee
Ali Samaiee
Type-safe event handling for Swift

emitter-kit v5.2.2 A replacement for NSNotificationCenter#addObserver and NSObject#addObserver that is type-safe and not verbose. import EmitterKit /

Alec Larson 570 Nov 25, 2022
A library for reactive and unidirectional Swift applications

ReactorKit is a framework for a reactive and unidirectional Swift application architecture. This repository introduces the basic concept of ReactorKit

ReactorKit 2.5k Dec 28, 2022
RxReduce is a lightweight framework that ease the implementation of a state container pattern in a Reactive Programming compliant way.

About Architecture concerns RxReduce Installation The key principles How to use RxReduce Tools and dependencies Travis CI Frameworks Platform Licence

RxSwift Community 125 Jan 29, 2022
Predictable state management for SwiftUI applications.

SwiftDux Predictable state management for SwiftUI applications. SwiftDux is a state container inspired by Redux and built on top of Combine and SwiftU

Steven Lambion 148 Jul 4, 2022
Lightweight Promises for Swift & Obj-C

Tomorrowland Tomorrowland is an implementation of Promises for Swift and Objective-C. A Promise is a wrapper around an asynchronous task that provides

Lily Ballard 115 Nov 23, 2022
When is a lightweight implementation of Promises in Swift

Description When is a lightweight implementation of Promises in Swift. It doesn't include any helper functions for iOS and OSX and it's intentional, t

Vadym Markov 259 Dec 29, 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
Swift Apps in a Swoosh! A modern framework for creating iOS apps, inspired by Redux.

Katana is a modern Swift framework for writing iOS applications' business logic that are testable and easy to reason about. Katana is strongly inspire

Bending Spoons 2.2k Jan 1, 2023
Bond is a Swift binding framework that takes binding concepts to a whole new level.

Bond, Swift Bond Update: Bond 7 has been released! Check out the migration guide to learn more about the update. Bond is a Swift binding framework tha

Declarative Hub 4.2k Jan 5, 2023
🐌 snail - An observables framework for Swift

?? snail A lightweight observables framework, also available in Kotlin Installation Carthage You can install Carthage with Homebrew using the followin

Compass 179 Nov 21, 2022