How to simplify UIApplication life cycle observation in iOS

Issue #375

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
final class LifecyclerHandler {
private var observer: AnyObject!
var action: (() -> Void)?
private let debouncer = Debouncer(delay: 1.0)

func setup() {
observer = NotificationCenter.default.addObserver(
forName: UIApplication.didBecomeActiveNotification,
object: nil,
queue: .main,
using: { [weak self] _ in
self?.debouncer.run {
self?.action?()
}
})
}
}
1
2
3
4
5
6
7
8
9
10
11
12
private let lifecycleHandler = LifecyclerHandler()

override func viewDidLoad() {
super.viewDidLoad()


lifecycleHandler.action = {
Deps.userHandler.refreshToken()
}

lifecycleHandler.setup()
}

Comments