diff --git a/Documentation/DemoApps/GRDBDemoiOS/GRDBDemoiOS/AppDelegate.swift b/Documentation/DemoApps/GRDBDemoiOS/GRDBDemoiOS/AppDelegate.swift index 672c383c0..3df9947f3 100644 --- a/Documentation/DemoApps/GRDBDemoiOS/GRDBDemoiOS/AppDelegate.swift +++ b/Documentation/DemoApps/GRDBDemoiOS/GRDBDemoiOS/AppDelegate.swift @@ -18,9 +18,5 @@ class AppDelegate: UIResponder, UIApplicationDelegate { .url(for: .applicationSupportDirectory, in: .userDomainMask, appropriateFor: nil, create: true) .appendingPathComponent("db.sqlite") dbQueue = try AppDatabase.openDatabase(atPath: databaseURL.path) - - // Be a nice iOS citizen, and don't consume too much memory - // See https://github.com/groue/GRDB.swift/blob/master/README.md#memory-management - dbQueue.setupMemoryManagement(in: application) } } diff --git a/GRDB/Core/DatabasePool.swift b/GRDB/Core/DatabasePool.swift index 50653f20f..210345431 100644 --- a/GRDB/Core/DatabasePool.swift +++ b/GRDB/Core/DatabasePool.swift @@ -24,10 +24,6 @@ public final class DatabasePool: DatabaseWriter { var databaseSnapshotCount = LockedBox(value: 0) - #if os(iOS) - private weak var application: UIApplication? - #endif - // MARK: - Database Information /// The path to the database. @@ -138,6 +134,12 @@ public final class DatabasePool: DatabaseWriter { } setupSuspension() + + // Be a nice iOS citizen, and don't consume too much memory + // See https://github.com/groue/GRDB.swift/#memory-management + #if canImport(UIKit) + setupMemoryManagement() + #endif } deinit { @@ -206,7 +208,7 @@ extension DatabasePool { } - #if os(iOS) + #if canImport(UIKit) /// Listens to UIApplicationDidEnterBackgroundNotification and /// UIApplicationDidReceiveMemoryWarningNotification in order to release /// as much memory as possible. @@ -214,8 +216,7 @@ extension DatabasePool { /// - param application: The UIApplication that will start a background /// task to let the database pool release its memory when the application /// enters background. - public func setupMemoryManagement(in application: UIApplication) { - self.application = application + private func setupMemoryManagement() { let center = NotificationCenter.default center.addObserver( self, @@ -231,7 +232,7 @@ extension DatabasePool { @objc private func applicationDidEnterBackground(_ notification: NSNotification) { - guard let application = application else { + guard let application = notification.object as? UIApplication else { return } diff --git a/GRDB/Core/DatabaseQueue.swift b/GRDB/Core/DatabaseQueue.swift index 99c3928a8..446c3785d 100644 --- a/GRDB/Core/DatabaseQueue.swift +++ b/GRDB/Core/DatabaseQueue.swift @@ -7,9 +7,6 @@ import UIKit /// A DatabaseQueue serializes access to an SQLite database. public final class DatabaseQueue: DatabaseWriter { private var writer: SerializedDatabase - #if os(iOS) - private weak var application: UIApplication? - #endif // MARK: - Configuration @@ -43,6 +40,12 @@ public final class DatabaseQueue: DatabaseWriter { defaultLabel: "GRDB.DatabaseQueue") setupSuspension() + + // Be a nice iOS citizen, and don't consume too much memory + // See https://github.com/groue/GRDB.swift/#memory-management + #if canImport(UIKit) + setupMemoryManagement() + #endif } /// Opens an in-memory SQLite database. @@ -83,7 +86,7 @@ extension DatabaseQueue { writer.sync { $0.releaseMemory() } } - #if os(iOS) + #if canImport(UIKit) /// Listens to UIApplicationDidEnterBackgroundNotification and /// UIApplicationDidReceiveMemoryWarningNotification in order to release /// as much memory as possible. @@ -91,8 +94,7 @@ extension DatabaseQueue { /// - param application: The UIApplication that will start a background /// task to let the database queue release its memory when the application /// enters background. - public func setupMemoryManagement(in application: UIApplication) { - self.application = application + private func setupMemoryManagement() { let center = NotificationCenter.default center.addObserver( self, @@ -108,7 +110,7 @@ extension DatabaseQueue { @objc private func applicationDidEnterBackground(_ notification: NSNotification) { - guard let application = application else { + guard let application = notification.object as? UIApplication else { return } diff --git a/README.md b/README.md index 768b36ece..4206346f6 100644 --- a/README.md +++ b/README.md @@ -7147,12 +7147,7 @@ This method blocks the current thread until all current database accesses are co **The iOS operating system likes applications that do not consume much memory.** -[Database queues](#database-queues) and [pools](#database-pools) can call the `releaseMemory` method for you, when application receives memory warnings, and when application enters background: call the `setupMemoryManagement` method after creating the queue or pool instance: - -``` -let dbQueue = try DatabaseQueue(...) -dbQueue.setupMemoryManagement(in: UIApplication.shared) -``` +[Database queues](#database-queues) and [pools](#database-pools) automatically calls the `releaseMemory` method for you, when application receives memory warnings, and when application enters background. ## Data Protection