Automatically setup memory management when UIKit is available

Signed-off-by: Pakkito <pe@pierlis.com>
This commit is contained in:
Pakkito committed 2020-02-28 17:21:27 +01:00
1 parent 2ff8c66b9e
commit 32675b38f1
4 files changed
+19 -25

No files matched your search

@@ -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)
}
}
+9 -8
View File
@@ -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
}
+9 -7
View File
@@ -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
}
+1 -6
View File
@@ -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