[英]How can i Update Cloud firestore document Field with the help DocumentID?
[英]Firestore encodable not including Firestore DocumentID
我在我的项目中使用 Firebase Firestore,我发现我需要使用 @DocumentId 将 firebase“_id”属性正确解码到我的可编码结构中。
我的问题是我需要保存编码的 object(目前我使用的是 userdefaults)并稍后在我的应用程序生命周期中重新解码 object。
当我使用 Firestore.encode 对我的 Firestore object 进行编码时,它会丢失 id 属性,因为它被编码为 [String: Any] 所以当我尝试解码获取的预编码 object 时,我无法这样做。
现在我单独存储 ID 属性,并在解码后将其重新添加到 object。
这是处理 Firestore 可编码的正确方法吗? 或者是否有更简洁的方法来编码然后解码 Firestore 编码的对象?
正如这里所讨论的
解决方案是显式请求 DocumentReference 的 DocumentID 包装,然后自己手动解包。 它看起来像这样:
ref = try container.decode(DocumentID<DocumentReference>.self, forKey: .ref).wrappedValue
另请检查这些类似案例以获取更多信息:
在 Firestore 中使用 Codable 时如何获取文档 ID?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.