[英]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.