diff --git a/GRDB/Record/FetchableRecord.swift b/GRDB/Record/FetchableRecord.swift index 9a11ca665..61130a4db 100644 --- a/GRDB/Record/FetchableRecord.swift +++ b/GRDB/Record/FetchableRecord.swift @@ -129,7 +129,9 @@ public protocol FetchableRecord { /// // A FetchableRecord + Decodable record /// struct Player: FetchableRecord, Decodable { /// // Customize the decoder name when decoding a database row - /// static let databaseDecodingUserInfo: [CodingUserInfoKey: Any] = [decoderName: "Database"] + /// static var databaseDecodingUserInfo: [CodingUserInfoKey: Any] { + /// [decoderName: "Database"] + /// } /// /// init(from decoder: Decoder) throws { /// // Print the decoder name @@ -146,6 +148,23 @@ public protocol FetchableRecord { /// decoder.userInfo = [decoderName: "JSON"] /// let player = try decoder.decode(Player.self, from: ...) /// ``` + /// + /// > Important: Make sure the `databaseDecodingUserInfo` property is + /// > explicitly declared as `[CodingUserInfoKey: Any]`. If it is not, + /// > the Swift compiler may silently miss the protocol requirement. + /// + /// > Important: Make sure the property is declared as a computed + /// > property (`static var`), instead of a stored property + /// > (`static let`). Computed properties avoid a compiler diagnostic + /// > with stored properties: + /// > + /// > ```swift + /// > // static property 'databaseDecodingUserInfo' is not + /// > // concurrency-safe because non-'Sendable' type + /// > // '[CodingUserInfoKey: Any]' may have shared + /// > // mutable state. + /// > static let databaseDecodingUserInfo: [CodingUserInfoKey: Any] = [decoderName: "Database"] + /// > ``` static var databaseDecodingUserInfo: [CodingUserInfoKey: Any] { get } /// Returns the `JSONDecoder` that decodes the value for a given column. diff --git a/README.md b/README.md index fadc39b59..8bfa61141 100644 --- a/README.md +++ b/README.md @@ -2797,7 +2797,9 @@ let player = try decoder.decode(Player.self, from: jsonData) ```swift extension Player: FetchableRecord { - static let databaseDecodingUserInfo: [CodingUserInfoKey: Any] = [decoderName: "database row"] + static var databaseDecodingUserInfo: [CodingUserInfoKey: Any] { + [decoderName: "database row"] + } } // prints "Decoded from database row" diff --git a/Tests/GRDBTests/FetchableRecordDecodableTests.swift b/Tests/GRDBTests/FetchableRecordDecodableTests.swift index f62e86408..1cbba07d8 100644 --- a/Tests/GRDBTests/FetchableRecordDecodableTests.swift +++ b/Tests/GRDBTests/FetchableRecordDecodableTests.swift @@ -1361,9 +1361,12 @@ extension FetchableRecordDecodableTests { context = decoder.userInfo[testKeyRoot] as? String } - static let databaseDecodingUserInfo: [CodingUserInfoKey: Any] = [ - testKeyRoot: "GRDB root", - testKeyNested: "GRDB column or scope"] + static var databaseDecodingUserInfo: [CodingUserInfoKey: Any] { + [ + testKeyRoot: "GRDB root", + testKeyNested: "GRDB column or scope", + ] + } static func databaseJSONDecoder(for column: String) -> JSONDecoder { let decoder = JSONDecoder() @@ -1666,7 +1669,9 @@ extension FetchableRecordDecodableTests { struct StructWithNestedType : PersistableRecord, FetchableRecord, Codable { static let databaseTableName = "t1" - static var databaseDecodingUserInfo: [CodingUserInfoKey: Any] = [CodingUserInfoKey.testKey: "correct"] + static var databaseDecodingUserInfo: [CodingUserInfoKey: Any] { + [CodingUserInfoKey.testKey: "correct"] + } let nested: NestedStruct? }