The Observer Pattern Alternatives
Compare with delegates and Combine publishers.
Observer Pattern Alternatives
NotificationCenter is one way to react to events. Swift offers others: delegation, Combine, and async streams. Each fits a different shape of communication.
The Observer Pattern
The observer pattern lets objects subscribe to events from a subject. NotificationCenter is a global, loosely coupled implementation of it.
import Foundation
// Subject posts, observers react, neither holds the other.
NotificationCenter.default.post(name: Notification.Name("tick"), object: nil)
print("Broadcast an event")All lessons in this course
- Posting and Observing Notifications
- Notification userInfo Payloads
- Removing Observers Safely
- The Observer Pattern Alternatives