简体   繁体   English

一次向 Firestore 添加大量文档(例如 1000 多个文档)时,应用程序挂起

[英]App hangs when adding a large number of documents to Firestore (e.g. 1000+ documents) at once

I am trying to allow a user to mass add a bunch of documents to Firestore from the client side.我试图允许用户从客户端向 Firestore 批量添加一堆文档。 It works fine when there aren't that many documents but when there a ton (eg 1000+ documents), it adds all of the documents and then just hangs.当没有那么多文档但有大量文档(例如 1000 多个文档)时,它可以正常工作,它会添加所有文档然后挂起。 If I close and then reopen the app, I see that all of the documents have been added as desired.如果我关闭然后重新打开应用程序,我会看到所有文档都已根据需要添加。

I am adding each document one by one using the following:我正在使用以下内容一一添加每个文档:

self.collection.document(noteID).setData(record.dictionary)

Is there a reason why this would hang after mass adding a bunch of documents but work fine on smaller sets?在大量添加一堆文档但在较小的集合上可以正常工作后,这是否会挂起? Is there a way to batch add a bunch of documents to Firestore so that they don't hang?有没有办法将一堆文档批量添加到 Firestore,以免它们挂起?

This happends because each write is generating a transaction that need to be executed, and your app hangs as it is waiting for each transaction to respond.这是因为每次写入都会生成一个需要执行的事务,并且您的应用程序在等待每个事务响应时挂起。

to write tons of documents in one action the recomendation in the official documentatipon is to use Batch Writes .在一个动作中编写大量文档,官方文档中的建议是使用Batch Writes

Just take in consideration that each batch can take up tpo 500 writes.只需考虑到每批可以占用 tpo 500 次写入。

to Add data on Batch writes you can do it like this:要在批量写入上添加数据,您可以这样做:

let batch = self.batch()

//set Documents to add 
let doc1 = self.collection(<COLLECTION>).document(noteID)
batch.setData(record.dictionary, forDocument: doc1)

//commit the batch
batch.commit() { err in
    if let err = err {
        print("Error writing batch \(err)")
    } else {
        print("Batch write succeeded.")
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 一次删除大量文档(例如 1000 多个文档)时,Firestore 删除挂起 - Firestore delete hangs when deleting a large number of documents (e.g. 1000+ documents) at once 离线时删除、添加和更新 Firestore 文档 - Deleting, adding and updating Firestore documents when offline 将Firestore文档添加到Pickerview时出错 - Error adding Firestore documents to Pickerview 在应用程序的第一次运行(例如教程)期间,如何使 uiviewcontroller 仅可见一次? - how can I make the uiviewcontroller visible only once during first run of the app (e.g. tutorial)? 如何在不阅读文档的情况下预先获取文档数量 - Firestore - Swift - how to get the number of documents upfront without reading documents - Firestore - Swift 如何只读取一次文件的数量? - How to read the number of documents only once? 非规范化数据时获取排序的 Firestore 文档 - Get sorted Firestore documents when denormalizing data 使用 Firestore 中的 getDocuments 在完成处理程序中一次获取所有文档 - Get all documents at once in a completion handler with getDocuments in Firestore Firestore-使用querySnaphot中的Int值一次创建多个文档 - Firestore - Creating multiple documents at once using Int value from querySnaphot Firestore侦听器正在获取比现有文档数更多的数据 - Firestore listener is fetching more data than the number of documents that exists
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM