From 33d9ca0e977cae3fd9bb17d7db4c2a3440d52daf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Wed, 8 Dec 2021 12:48:19 +0100 Subject: [PATCH] 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 --- GRDB/Core/DatabasePool.swift | 9 +++++++-- GRDB/Core/DatabaseQueue.swift | 9 +++++++-- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/GRDB/Core/DatabasePool.swift b/GRDB/Core/DatabasePool.swift index 78bf84cfc..0a9e25b6e 100644 --- a/GRDB/Core/DatabasePool.swift +++ b/GRDB/Core/DatabasePool.swift @@ -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) } diff --git a/GRDB/Core/DatabaseQueue.swift b/GRDB/Core/DatabaseQueue.swift index 86799245b..2b4ddad05 100644 --- a/GRDB/Core/DatabaseQueue.swift +++ b/GRDB/Core/DatabaseQueue.swift @@ -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) }