Skip to content

Commit d72b710

Browse files
committed
DatabasePool automatic memory management no longer uses any reader barrier
This fixes the failing `testDatabasePoolDoesNotPreventConcurrentReadsOnPressureEvent` test
1 parent e1a63a4 commit d72b710

File tree

2 files changed

+14
-12
lines changed

2 files changed

+14
-12
lines changed

GRDB/Core/Configuration.swift

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -276,15 +276,11 @@ public struct Configuration {
276276
/// Default: nil
277277
public var writeTargetQueue: DispatchQueue? = nil
278278

279+
#if os(iOS)
279280
/// Sets whether GRDB will release memory when entering the background or
280281
/// upon receiving a memory warning in iOS.
281282
///
282-
/// Enabling this setting may help keep iOS from terminating your app when
283-
/// memory pressure becomes high. However, it can also cause database
284-
/// readers to block longer than they normally would.
285-
///
286283
/// Default: true
287-
#if os(iOS)
288284
public var automaticMemoryManagement = true
289285
#endif
290286

GRDB/Core/DatabasePool.swift

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -252,22 +252,28 @@ extension DatabasePool {
252252

253253
let task: UIBackgroundTaskIdentifier = application.beginBackgroundTask(expirationHandler: nil)
254254
if task == .invalid {
255-
// Perform releaseMemory() synchronously.
255+
// Release memory synchronously
256256
releaseMemory()
257257
} else {
258-
// Perform releaseMemory() asynchronously.
259-
DispatchQueue.global().async {
260-
self.releaseMemory()
258+
// Release memory eventually.
259+
//
260+
// We don't know when reader connections will be closed (because
261+
// they may be currently in use), so we don't quite know when
262+
// reader memory will be freed (which would be the ideal timing for
263+
// ending our background task).
264+
//
265+
// So let's just end the background task after the writer connection
266+
// has freed its memory. That's better than nothing.
267+
releaseMemoryEventually()
268+
writer.async { _ in
261269
application.endBackgroundTask(task)
262270
}
263271
}
264272
}
265273

266274
@objc
267275
private func applicationDidReceiveMemoryWarning(_ notification: NSNotification) {
268-
DispatchQueue.global().async {
269-
self.releaseMemory()
270-
}
276+
releaseMemoryEventually()
271277
}
272278
#endif
273279
}

0 commit comments

Comments
 (0)