diff --git a/GRDB/Core/Concurrency/DatabasePool.swift b/GRDB/Core/Concurrency/DatabasePool.swift index 23cb940a1..7f41ae125 100644 --- a/GRDB/Core/Concurrency/DatabasePool.swift +++ b/GRDB/Core/Concurrency/DatabasePool.swift @@ -243,24 +243,28 @@ extension DatabasePool { return } - let task: UIBackgroundTaskIdentifier = application.beginBackgroundTask(expirationHandler: nil) - if task == .invalid { - // Release memory synchronously - releaseMemory() - } else { - // Release memory eventually. - // - // We don't know when reader connections will be closed (because - // they may be currently in use), so we don't quite know when - // reader memory will be freed (which would be the ideal timing for - // ending our background task). - // - // So let's just end the background task after the writer connection - // has freed its memory. That's better than nothing. - releaseMemoryEventually() - writer.async { _ in - DispatchQueue.main.async { - application.endBackgroundTask(task) + // `UIApplication.beginBackgroundTask(expirationHandler:)` is MainActor-isolated. + DispatchQueue.main.async { [weak self] in + guard let self else { return } + + let taskId = application.beginBackgroundTask(expirationHandler: nil) + if taskId == .invalid { + releaseMemory() + } else { + // Release memory eventually. + // + // We don't know when reader connections will be closed (because + // they may be currently in use), so we don't quite know when + // reader memory will be freed (which would be the ideal timing for + // ending our background task). + // + // So let's just end the background task after the writer connection + // has freed its memory. That's better than nothing. + releaseMemoryEventually() + writer.async { _ in + DispatchQueue.main.async { + application.endBackgroundTask(taskId) + } } } } diff --git a/GRDB/Core/Concurrency/DatabaseQueue.swift b/GRDB/Core/Concurrency/DatabaseQueue.swift index cb4e49448..a71e7ce13 100644 --- a/GRDB/Core/Concurrency/DatabaseQueue.swift +++ b/GRDB/Core/Concurrency/DatabaseQueue.swift @@ -169,16 +169,20 @@ extension DatabaseQueue { return } - let task: UIBackgroundTaskIdentifier = application.beginBackgroundTask(expirationHandler: nil) - if task == .invalid { - // Release memory synchronously - releaseMemory() - } else { - // Release memory asynchronously - writer.async { db in - db.releaseMemory() - DispatchQueue.main.async { - application.endBackgroundTask(task) + // `UIApplication.beginBackgroundTask(expirationHandler:)` is MainActor-isolated. + DispatchQueue.main.async { [weak self] in + guard let self else { return } + + let taskId: UIBackgroundTaskIdentifier = application.beginBackgroundTask(expirationHandler: nil) + if taskId == .invalid { + releaseMemory() + } else { + // Release memory asynchronously + writer.async { db in + db.releaseMemory() + DispatchQueue.main.async { + application.endBackgroundTask(taskId) + } } } }