Skip to content

Commit

Permalink
add example code of notification
Browse files Browse the repository at this point in the history
  • Loading branch information
p-x9 committed Mar 5, 2023
1 parent 9581ebe commit 05d3ff0
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions Example/Example/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ class ViewController: UIViewController {
}

var orderedDictionary: OrderedDictionary<String, Any> = .init()

private let notificationCenter = NotificationCenter.default
private var notificationObservations = [Any]()

override func viewDidLoad() {
super.viewDidLoad()
Expand All @@ -40,11 +43,14 @@ class ViewController: UIViewController {
super.viewWillAppear(animated)

title = appContainer.activeContainer?.name

registerNotifications()
}

override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)

unregisterNotifications()
}

private func setupViews() {
Expand Down Expand Up @@ -273,6 +279,26 @@ extension ViewController: UITableViewDelegate {
}
}

extension ViewController {
func registerNotifications() {
let willChangeObservation = notificationCenter.addObserver(forName: AppContainer.containerWillChangeNotification, object: nil, queue: .current) { _ in
print("container will change")
}

let didChangeObservation = notificationCenter.addObserver(forName: AppContainer.containerWillChangeNotification, object: nil, queue: .current) { _ in
print("container did change")
}

self.notificationObservations = [willChangeObservation, didChangeObservation]
}

func unregisterNotifications() {
notificationObservations.forEach {
notificationCenter.removeObserver($0)
}
}
}


extension ViewController {
class View: UIView {
Expand Down

0 comments on commit 05d3ff0

Please sign in to comment.