From 2aabc4c10e80aaf8f77487f5e4187ab02793794f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Sun, 31 Mar 2024 07:54:07 +0200 Subject: [PATCH] OnDemandFuture fulfill is Sendable --- GRDB/Core/DatabaseReader.swift | 8 +++----- GRDB/Utils/OnDemandFuture.swift | 10 +++++++--- TODO.md | 3 +++ 3 files changed, 13 insertions(+), 8 deletions(-) diff --git a/GRDB/Core/DatabaseReader.swift b/GRDB/Core/DatabaseReader.swift index 4dc901253..2295f6f03 100644 --- a/GRDB/Core/DatabaseReader.swift +++ b/GRDB/Core/DatabaseReader.swift @@ -555,11 +555,9 @@ extension DatabaseReader { value: @escaping (Database) throws -> Output) -> DatabasePublishers.Read { - Deferred { - Future { fulfill in - self.asyncRead { dbResult in - fulfill(dbResult.flatMap { db in Result { try value(db) } }) - } + OnDemandFuture { fulfill in + self.asyncRead { dbResult in + fulfill(dbResult.flatMap { db in Result { try value(db) } }) } } .receiveValues(on: scheduler) diff --git a/GRDB/Utils/OnDemandFuture.swift b/GRDB/Utils/OnDemandFuture.swift index e1d8dff6c..645da8d19 100644 --- a/GRDB/Utils/OnDemandFuture.swift +++ b/GRDB/Utils/OnDemandFuture.swift @@ -14,9 +14,12 @@ import Foundation /// Both two extra scheduling guarantees are used by GRDB in order to be /// able to spawn concurrent database reads right from the database writer /// queue, and fulfill GRDB preconditions. +/// +/// OnDemandFuture also adds Sendable requirements that avoid +/// compiler warnings. @available(iOS 13, macOS 10.15, tvOS 13, *) struct OnDemandFuture: Publisher { - typealias Promise = (Result) -> Void + typealias Promise = @Sendable (Result) -> Void typealias Output = Output typealias Failure = Failure fileprivate let attemptToFulfill: (@escaping Promise) -> Void @@ -34,8 +37,9 @@ struct OnDemandFuture: Publisher { } @available(iOS 13, macOS 10.15, tvOS 13, *) -private class OnDemandFutureSubscription: Subscription { - typealias Promise = (Result) -> Void +private class OnDemandFutureSubscription: Subscription, @unchecked Sendable { + // @unchecked because `state` is protected with `lock`. + typealias Promise = @Sendable (Result) -> Void private enum State { case waitingForDemand(downstream: Downstream, attemptToFulfill: (@escaping Promise) -> Void) diff --git a/TODO.md b/TODO.md index 3f9c5d9ac..557fff135 100644 --- a/TODO.md +++ b/TODO.md @@ -97,6 +97,9 @@ - [X] GRDB7: All writes should be immediate, right? https://github.com/groue/GRDB.swift/pull/1485#issuecomment-1953793553 - [X] GRDB7: Remove concurrenRead and DatabaseFuture - [ ] GRDB7: Sendable conformances + - [ ] SerializedDatabase Sendable async closure + - [X] OnDemandFuture Sendable async closure + - [ ] WALSnapshotTransaction refactor - [X] anydatabasecancellable - [ ] anydatabaseregionconvertible - [ ] anyfetchrequest