mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-29 04:24:21 +00:00
Asynchronous registration to UIApplication.didReceiveMemoryWarningNotification
This commit is contained in:
1 parent
e7eac0d500
commit
d66e069822
3 files changed
+28
-16
No files matched your search
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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 }
|
||||
|
||||
Reference in new issue
Block a user