File tree Expand file tree Collapse file tree 2 files changed +14
-12
lines changed Expand file tree Collapse file tree 2 files changed +14
-12
lines changed Original file line number Diff line number Diff line change @@ -276,15 +276,11 @@ public struct Configuration {
276
276
/// Default: nil
277
277
public var writeTargetQueue : DispatchQueue ? = nil
278
278
279
+ #if os(iOS)
279
280
/// Sets whether GRDB will release memory when entering the background or
280
281
/// upon receiving a memory warning in iOS.
281
282
///
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
- ///
286
283
/// Default: true
287
- #if os(iOS)
288
284
public var automaticMemoryManagement = true
289
285
#endif
290
286
Original file line number Diff line number Diff line change @@ -252,22 +252,28 @@ extension DatabasePool {
252
252
253
253
let task : UIBackgroundTaskIdentifier = application. beginBackgroundTask ( expirationHandler: nil )
254
254
if task == . invalid {
255
- // Perform releaseMemory() synchronously.
255
+ // Release memory synchronously
256
256
releaseMemory ( )
257
257
} 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
261
269
application. endBackgroundTask ( task)
262
270
}
263
271
}
264
272
}
265
273
266
274
@objc
267
275
private func applicationDidReceiveMemoryWarning( _ notification: NSNotification ) {
268
- DispatchQueue . global ( ) . async {
269
- self . releaseMemory ( )
270
- }
276
+ releaseMemoryEventually ( )
271
277
}
272
278
#endif
273
279
}
You can’t perform that action at this time.
0 commit comments