Notification_Pattern

Notification Pattern

Add Observer

Adding the notification observer is easy and we use singleton pattern to get default context from Notification Center. We can specify the name or class object
You can specify the closure to expand right away or pass
in another closure

func listenObserver() {
    NotificationCenter
    .default
    .addObserver(
	    forName: Notification.Name(rawValue: notificationName),
	    object: object, 
	    queue: nil, 
	    using: closure
	)
}

Selector option with addObserver

NotificationCenter
    .default
    .addObserver(
	    self,
	    selector: #selector(post), 
	    name: Notification.Name(rawValue: notificationName),
	    object: nil
	)

@objc 
func post() { }

Remove Observer

Normal removal of observer

NotificationCenter.default.removeObserver(self)
NotificationCenter.default.removeObserver(Notification.Name)

When removing an observer, remove it with the most specific detail possible. For example, if you used a name and object to register the observer, use removeObserver(_:name:object:) with the name and object.

apple doc

Maintain Array of Notification Observers

class NotificationBusContainer : NSObject { 

    // Variables to manage Notification state
    private let postingQueue = DispatchQueue(label: "com.companyName.projectName.notificationBusContainer",qos: .utility)
    /// Array of Notifications
    private var observerTokens: [NSObjectProtocol]
    /// Extra class ref to be passed around in Notifications
    private var otherClassRef: CustomClass()
    /// Bubble up event / Process Events
    private var processBus = ProcessEventsBus()
    
    init () { 
        self.observerTokens = [NSObjectProtocol](/404)
        super.init()
    }
    
    deinit {
        postingQueue.sync {
            unhookNotifications()
        }
    }

    /// Public function for explicit notifications state reset
    @objc public func disconnect() {
        postingQueue.sync {
            unhookNotifications()
        }
    }

    /// Internal func where we remove all notification observers
    func unhookNotifications() {
        for observerToken in observerTokens { 
            NotificationCenter
            .default
            .removeObserver(observerToken)
        }
        observerTokens.removeAll()
    }

    /// Internal convenience func for adding notifications to the internal Notification array state.
    func addObserver(forName notificationName: String,
    withObject object: Any?,
    withClosure closure: @escaping (Notification) -> ()) {
        let token = NotificationCenter
        .default
        .addObserver(
            forName: Notification
                .Name(rawValue:notificationName),
            object: object,
            queue: nil,
            using: closure)
            
        observerTokens.append(token)
    }


    func addCustomNotification() {
        addObserver(forName: "KAUTILYA_CUSTOM_NS_NAME", withObject: otherClassRef) { [weak self] otherClass in
            // Extract info 
            let data = otherClass.someData()
            self?.postingQueue.async { [weak self] in
                // Do something with Notification closure
                self?.processBus.sendEvent(data)
            }
        }
    }

}

Combine

Listen Lifecycle event changes

import UIKit
import Combine

class MyFunkyViewController: UIViewController {

    /// The cancel bag containing all the subscriptions.
    private var cancelBag: Set<AnyCancellable> = []

    override func viewDidLoad() {
        super.viewDidLoad()
        addSubscribers()
    }

    /// Adds all the subscribers.
    private func addSubscribers() {
        NotificationCenter
            .Publisher(center: .default,
                       name: UIApplication.willEnterForegroundNotification)
            .sink { [weak self] _ in
                self?.doSomething()
            }
            .store(in: &cancelBag)
    }

    /// Called when entering foreground.
    private func doSomething() {
        print("Hello foreground!")
    }

	func deinit() {
		cancelBag.removeAll()
	}
}

SO | triggering action app-enters-foreground-from-a-local-notific

Resources

Medium | Notifications in swift

Apple Docs | XPC Inter-process communication