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 Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s