NotificationCenter and Combine

Using NotificationCenter and the Combine Framework.

1. Define extension on NSNotification.Name

extension NSNotification.Name {
    static let fetchedData = Self(rawValue: "fetchedData")
}

2. Post the Notification

NotificationCenter.default.post(name: .fetchedData, object: nil)

3. Subscribe to the Notification

import Combine 

private var cancellables = Set<AnyCancellable>()
 
NotificationCenter.default.publisher(for: .fetchedData)
    .sink { _ in
        print("fetchedData successfully")

    }
    .store(in: &cancellables)

Leave a comment