FirestoreSwift 文档参考可编码

问题描述 投票:0回答:1

我有以下型号:

struct Organization: Codable, Identifiable {
  @DocumentID var id: String? <- Identifiable 
  let ref: DocumentReference <- Reference to the document
  let name: String
  

  enum CodingKeys: String, CodingKey {
    case name = "Name"
  }
}

我需要在模型中使用 documentRef 来在不同数据库和不同子集合之间进行引用。

如何映射引用并使其可编码,因为它不是字段。

此外我还收到错误

Unknown attribute 'DocumentID'

使用 Firebase SDK:10.20.0

swift firebase google-cloud-firestore codable
1个回答
0
投票

有点不一样

struct Organization: Codable, Identifiable {
  @DocumentID var ref: DocumentReference? //This will be auto-populated when you fetch/Decode.
  var id: String? {
      ref?.id
  }
  let name: String
  

  enum CodingKeys: String, CodingKey {
    case name = "Name"
  }
}

确保您已导入

FirebaseFirestoreSwift

© www.soinside.com 2019 - 2024. All rights reserved.