UIApplication notification names are isolated to @MainActor

I believe this is incorrect: FB9801372 (UIApplication.didReceiveMemoryWarningNotification should not be declared @MainActor)

Meanwhile, just use the string version.

A workaround based on a MainActor-isolated task can't work due to conflict between compiler version and minimum iOS target:

    #if compiler(>=5.5) && canImport(_Concurrency)
    if #available(...) { // Check for availability of concurrency runtime
        Task { @MainActor [weak self] in
            NotificationCenter.default.addObserver(..., name: UIApplication.didReceiveMemoryWarningNotification, object: nil)
        }
    } else {
        // IMPOSSIBLE CASE: No access to Task, no access to notification names
    }
    #else
    NotificationCenter.default.addObserver(..., name: UIApplication.didReceiveMemoryWarningNotification, object: nil)
    #endif
This commit is contained in:
Gwendal Roué committed 2021-12-08 12:48:19 +01:00
1 parent b2985b77c4
commit 33d9ca0e97
2 files changed
+14 -4

No files matched your search

+7 -2
View File
@@ -183,15 +183,20 @@ extension DatabasePool {
/// as much memory as possible.
private func setupMemoryManagement() {
let center = NotificationCenter.default
// Use raw notification names because of
// FB9801372 (UIApplication.didReceiveMemoryWarningNotification should not be declared @MainActor)
// TODO: Reuse UIApplication.didReceiveMemoryWarningNotification when possible.
// TODO: Reuse UIApplication.didEnterBackgroundNotification when possible.
center.addObserver(
self,
selector: #selector(DatabasePool.applicationDidReceiveMemoryWarning(_:)),
name: UIApplication.didReceiveMemoryWarningNotification,
name: NSNotification.Name(rawValue: "UIApplicationDidReceiveMemoryWarningNotification"),
object: nil)
center.addObserver(
self,
selector: #selector(DatabasePool.applicationDidEnterBackground(_:)),
name: UIApplication.didEnterBackgroundNotification,
name: NSNotification.Name(rawValue: "UIApplicationDidEnterBackgroundNotification"),
object: nil)
}
+7 -2
View File
@@ -92,15 +92,20 @@ extension DatabaseQueue {
/// as much memory as possible.
private func setupMemoryManagement() {
let center = NotificationCenter.default
// Use raw notification names because of
// FB9801372 (UIApplication.didReceiveMemoryWarningNotification should not be declared @MainActor)
// TODO: Reuse UIApplication.didReceiveMemoryWarningNotification when possible.
// TODO: Reuse UIApplication.didEnterBackgroundNotification when possible.
center.addObserver(
self,
selector: #selector(DatabaseQueue.applicationDidReceiveMemoryWarning(_:)),
name: UIApplication.didReceiveMemoryWarningNotification,
name: NSNotification.Name(rawValue: "UIApplicationDidReceiveMemoryWarningNotification"),
object: nil)
center.addObserver(
self,
selector: #selector(DatabaseQueue.applicationDidEnterBackground(_:)),
name: UIApplication.didEnterBackgroundNotification,
name: NSNotification.Name(rawValue: "UIApplicationDidEnterBackgroundNotification"),
object: nil)
}