[英]How to upload documents to Firestore
有一個關於從 Firebase Firestore Cloud 加載數據的問題。 有一個簡單的查詢:
val firestore = Firebase.firestore
suspend fun getDocuments(timestamp: Timestamp): List<Entity> =
firestore
.collection("collection")
.orderBy("modified_at", Query.Direction.ASCENDING)
.whereGreaterThan("modified_at", timestamp)
.get()
.await()
.toObjects(Entity::class.java)
如果上傳文件時出現問題怎么辦? 我會收到不完整的數據或錯誤嗎?
文件按什么順序加載? orderBy 會影響加載順序嗎?
如果上傳文件時出現問題怎么辦?
據我所知,您沒有上傳任何文檔,您只是使用get()閱讀它們:
執行查詢並將結果作為 QuerySnapshot 返回。
在 Firestore 中執行 CRUD 操作時,總是存在操作失敗的可能性,例如,由於不正確的安全規則。 話雖如此,始終建議處理錯誤。 所以你的方法應該包含一個 try-catch 如下所示:
suspend fun getDocuments(timestamp: Timestamp): List<Entity> {
return try {
firestore
.collection("collection")
.orderBy("modified_at", Query.Direction.ASCENDING)
.whereGreaterThan("modified_at", timestamp)
.get()
.await()
.toObjects(Entity::class.java)
} catch (e: Exception) {
throw e
}
}
然而,更優雅的解決方案是使用包含兩個字段、數據和異常的 class。 以下資源中解釋了這種做法:
文件按什么順序加載?
您可以根據作為參數傳遞給orderBy()
方法的內容對文檔進行排序。
orderBy 會影響加載順序嗎?
當然,這就是我們使用它的原因,有一個特定的順序。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.