From d66e069822a12ed76b9501f865bc46a428446a2d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Fri, 3 Nov 2023 11:02:37 +0100 Subject: [PATCH] Asynchronous registration to UIApplication.didReceiveMemoryWarningNotification --- GRDB/Core/DatabasePool.swift | 7 +++-- GRDB/Core/DatabaseQueue.swift | 7 +++-- .../DatabasePoolReleaseMemoryTests.swift | 30 +++++++++++-------- 3 files changed, 28 insertions(+), 16 deletions(-) diff --git a/GRDB/Core/DatabasePool.swift b/GRDB/Core/DatabasePool.swift index 8a9f40e62..fc012a88f 100644 --- a/GRDB/Core/DatabasePool.swift +++ b/GRDB/Core/DatabasePool.swift @@ -91,7 +91,9 @@ public final class DatabasePool { // See https://github.com/groue/GRDB.swift/#memory-management #if os(iOS) if configuration.automaticMemoryManagement { - setupMemoryManagement() + DispatchQueue.main.async { [weak self] in + self?.setupMemoryManagement() + } } #endif } @@ -221,6 +223,7 @@ extension DatabasePool { /// Listens to UIApplicationDidEnterBackgroundNotification and /// UIApplicationDidReceiveMemoryWarningNotification in order to release /// as much memory as possible. + @MainActor private func setupMemoryManagement() { let center = NotificationCenter.default center.addObserver( @@ -235,7 +238,7 @@ extension DatabasePool { object: nil) } - @objc + @MainActor @objc private func applicationDidEnterBackground(_ notification: NSNotification) { guard let application = notification.object as? UIApplication else { return diff --git a/GRDB/Core/DatabaseQueue.swift b/GRDB/Core/DatabaseQueue.swift index fa9105a01..25360a376 100644 --- a/GRDB/Core/DatabaseQueue.swift +++ b/GRDB/Core/DatabaseQueue.swift @@ -65,7 +65,9 @@ public final class DatabaseQueue { // See https://github.com/groue/GRDB.swift/#memory-management #if os(iOS) if configuration.automaticMemoryManagement { - setupMemoryManagement() + DispatchQueue.main.async { [weak self] in + self?.setupMemoryManagement() + } } #endif } @@ -141,6 +143,7 @@ extension DatabaseQueue { /// Listens to UIApplicationDidEnterBackgroundNotification and /// UIApplicationDidReceiveMemoryWarningNotification in order to release /// as much memory as possible. + @MainActor private func setupMemoryManagement() { let center = NotificationCenter.default center.addObserver( @@ -155,7 +158,7 @@ extension DatabaseQueue { object: nil) } - @objc + @MainActor @objc private func applicationDidEnterBackground(_ notification: NSNotification) { guard let application = notification.object as? UIApplication else { return diff --git a/Tests/GRDBTests/DatabasePoolReleaseMemoryTests.swift b/Tests/GRDBTests/DatabasePoolReleaseMemoryTests.swift index 9b6bb306c..4496d1f2e 100644 --- a/Tests/GRDBTests/DatabasePoolReleaseMemoryTests.swift +++ b/Tests/GRDBTests/DatabasePoolReleaseMemoryTests.swift @@ -57,10 +57,12 @@ class DatabasePoolReleaseMemoryTests: GRDBTestCase { // Precondition: there is one reader. try dbPool.read { _ in } - // Simulate memory warning. - NotificationCenter.default.post( - name: UIApplication.didReceiveMemoryWarningNotification, - object: nil) + // Eventually simulate memory warning (after dbPool could register). + DispatchQueue.main.async { + NotificationCenter.default.post( + name: UIApplication.didReceiveMemoryWarningNotification, + object: nil) + } // Postcondition: reader connection was closed withExtendedLifetime(dbPool) { _ in @@ -85,10 +87,12 @@ class DatabasePoolReleaseMemoryTests: GRDBTestCase { // Precondition: there is one reader. try dbPool.read { _ in } - // Simulate memory warning. - NotificationCenter.default.post( - name: UIApplication.didReceiveMemoryWarningNotification, - object: nil) + // Eventually simulate memory warning (after dbPool could register). + DispatchQueue.main.async { + NotificationCenter.default.post( + name: UIApplication.didReceiveMemoryWarningNotification, + object: nil) + } // Postcondition: no reader connection was closed withExtendedLifetime(dbPool) { _ in @@ -106,10 +110,12 @@ class DatabasePoolReleaseMemoryTests: GRDBTestCase { semaphore.wait() } - // Simulate memory warning. - NotificationCenter.default.post( - name: UIApplication.didReceiveMemoryWarningNotification, - object: nil) + // Eventually simulate memory warning (after dbPool could register). + DispatchQueue.main.async { + NotificationCenter.default.post( + name: UIApplication.didReceiveMemoryWarningNotification, + object: nil) + } // Make sure we can read try dbPool.read { _ in }