StrictConcurrency: UIApplication is MainActor-isolated

This commit is contained in:
Gwendal Roué committed 2024-07-27 16:37:33 +02:00
1 parent 3995bfdbec
commit 991d2c8bbc
2 files changed
+36 -28

No files matched your search

+22 -18
View File
@@ -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)
}
}
}
}
+14 -10
View File
@@ -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)
}
}
}
}