Skip to content

Commit e006f4e

Browse files
authored
Remove observeOnce method that’s not necessary
1 parent c9d1f7a commit e006f4e

File tree

2 files changed

+8
-43
lines changed

2 files changed

+8
-43
lines changed

Signal/Preconditions/AppActivePrecondition.swift

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,17 @@
66
import Foundation
77
import SignalServiceKit
88

9-
class AppActivePrecondition: Precondition {
10-
private let appContext: AppContext
9+
struct AppActivePrecondition: Precondition {
10+
private let _precondition: NotificationPrecondition
11+
1112
init(appContext: AppContext) {
12-
self.appContext = appContext
13+
self._precondition = NotificationPrecondition(
14+
notificationName: UIApplication.didBecomeActiveNotification,
15+
isSatisfied: { appContext.isAppForegroundAndActive() },
16+
)
1317
}
1418

15-
@MainActor
1619
func waitUntilSatisfied() async -> WaitResult {
17-
if appContext.isAppForegroundAndActive() {
18-
return .satisfiedImmediately
19-
}
20-
await NotificationCenter.default.observeOnce(UIApplication.didBecomeActiveNotification)
21-
return .wasNotSatisfiedButIsNow
20+
return await self._precondition.waitUntilSatisfied()
2221
}
2322
}

SignalServiceKit/Util/NSNotificationCenter+OWS.swift

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -12,40 +12,6 @@ import Foundation
1212
// possible risk of deadlock. These methods also ensure that
1313
// the notifications are always fired on the main thread.
1414
extension NotificationCenter {
15-
16-
/// Waits until `name`/`object` is posted.
17-
public func observeOnce(_ name: Notification.Name, object: Any? = nil) async {
18-
await withCheckedContinuation { continuation in
19-
// Concurrency in this method is nontrivial. There are at least two
20-
// relevant race conditions:
21-
//
22-
// (1) Multiple threads could post a notification at the same time, and
23-
// this will trigger multiple callbacks, even if you call `removeObserver`
24-
// as quickly as possible. We guard against this with an atomic
25-
// compare-and-swap to set the observer to nil.
26-
//
27-
// (2) When initially registering the observer, it's possible that another
28-
// thread could be posting the notifiation at the same time. If the
29-
// notification callback happens before we've assigned the initial value to
30-
// the atomic observer value, we'll drop that notification. We avoid that
31-
// problem by ensuring that the `addObserver` call and assignment of the
32-
// observer happen atomically.
33-
//
34-
// Memory management in this method is also non-trivial. The observer
35-
// captured in the block must be set to nil to avoid leaking memory.
36-
let observer = AtomicOptional<NSObjectProtocol>(nil, lock: .init())
37-
_ = observer.map { _ in
38-
return addObserver(forName: name, object: object, queue: nil, using: { [weak self] notification in
39-
guard let observer = observer.swap(nil) else {
40-
return
41-
}
42-
self?.removeObserver(observer)
43-
continuation.resume(returning: ())
44-
})
45-
}
46-
}
47-
}
48-
4915
public func postOnMainThread(_ notification: Notification) {
5016
DispatchQueue.main.async {
5117
self.post(notification)

0 commit comments

Comments
 (0)