【问题标题】:Add multiple documents and receive the ID in Firestore添加多个文档并在 Firestore 中接收 ID
【发布时间】:2020-08-28 04:35:42
【问题描述】:

在 Firestore 中将多个保存操作作为单个批次执行后,如何读取文档列表的结果?

发送到 Firestore 后,我需要接收文档数据以删除 SQLite 中的注册。我的应用程序需要离线工作并在我有互联网时一次发送所有文档。

我是这样发送的(代码如下),但是发送所有数据需要很长时间

    List<Record> notUpdate = recordService.getNotUpdate();

    for (Record record : notUpdate) {

        Map<String, Object> field = new HashMap<>();
        field.put("operator", driverService.getByID(record.getFk_driver()).getName());
        field.put("operator_id", driverService.getByID(record.getFk_driver()).getUid());
        field.put("status", record.getStatus());
        field.put("fleet_type", sessionService.getDevice().getFkFleet());
        field.put("equipment", sessionService.getEquipment().getName());
        field.put("equipment_id", sessionService.getEquipment().getUid());
        field.put("device_id", sessionService.getDevice().getUid());
        field.put("date", DateUtilService.convertLongToTimestamp(record.getDate()));
        field.put("location", new GeoPoint(10, 20));

        collection.add(field)
                .addOnSuccessListener(documentReference -> {
                    recordService.delete(record);

                    if (activity instanceof MainActivity) {
                        Listener listener = (Listener) activity;
                        listener.onRecordUpdateListener();
                    }
                })
                .addOnFailureListener(Throwable::printStackTrace);
    }

我阅读了Firestore multiple write operations,但似乎使用 WriteBatch 我无法阅读文档,只需发送它们即可。如果有人知道另一种解决方案,我将不胜感激。

【问题讨论】:

  • 请编辑您的问题并添加从 Firestore 读取数据的代码。我只看到Record 对象的列表。
  • 这样我可以使用 Record 对象删除,因为我一次发送一个,但我仍然在 addOnSuccessListener 中收到 documentReference 以及插入文档中的数据。

标签: android firebase sqlite google-cloud-firestore


【解决方案1】:

你说的是真的——一个批处理只能写数据。它无法读取它们。如果您想在写入后读取文档,则必须等待批量写入完成,然后执行查询以读取写入的文档。没有简单的解决方法。

【讨论】:

    猜你喜欢
    • 2020-12-04
    • 1970-01-01
    • 2020-09-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-27
    • 2018-12-05
    • 2020-11-23
    相关资源
    最近更新 更多