首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >修复云函数数组循环-写入长延迟

修复云函数数组循环-写入长延迟
EN

Stack Overflow用户
提问于 2019-04-19 14:50:35
回答 2查看 516关注 0票数 0

我有一个云函数,它遍历一个uids数组,然后为每个uids创建一个写。正常的写几乎是瞬间发生的,但是在写出来之前还有很长的时间。

我试过不同尺寸的阵列。只有一两个uids的数组速度更快,但仍有5-6秒的滞后。

代码语言:javascript
复制
exports.addPostings = functions.firestore
    .document('posts/{postID}')
    .onCreate((snap, context) => {
    const newValue = snap.data();
    var uid = newValue.author.uid;
    let followers = [];
    var feedRef = db.collection("feedItems");
    var authorRef = db.collection("users").doc(newValue.author.uid);
    authorRef.get().then((doc) => {
        let data = doc.data();
        let post_count = data.postCount;
        authorRef.update({
            postCount: Number(post_count) + 1
        }).then(() => {
            authorRef.collection('followers').doc('content').get().then((doc) => {
                let data = doc.data();
                if (typeof data.uids != 'undefined') {
                    followers = data.uids;
                }
            }).then(() => {
                followers.forEach((fol) => {
                  feedRef.add({
                      createdAt: admin.firestore.FieldValue.serverTimestamp(), uid: fol, creatorUid: uid,
                      postId: context.params.postID, isResharedPost: false, wasViewed: false,
                      wasReshared: false, wasLiked: false, wasDirectlyShared: false
                  });
                });
            });
        });
    });
});
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-04-19 17:08:12

您应该在代码中修改以下几点:

  1. 您不会返回异步方法()返回的承诺,这是编写云函数代码时的关键,正如官方Firebase视频系列:https://firebase.google.com/docs/functions/video-series/中关于"JavaScript承诺“的3段视频所解释的那样
  2. 你应该正确地连锁承诺。
  3. 您应该使用批次写入,如CuriousGeorge所示

下面的修改应该能做到这一点(但是没有测试!):

代码语言:javascript
复制
exports.addPostings = functions.firestore
  .document('posts/{postID}')
  .onCreate((snap, context) => {
    const newValue = snap.data();
    var uid = newValue.author.uid;
    let followers = [];
    var feedRef = db.collection('feedItems');
    var authorRef = db.collection('users').doc(newValue.author.uid);

    return authorRef
      .get()
      .then(doc => {
        let data = doc.data();
        let post_count = data.postCount;
        return authorRef.update({
          postCount: Number(post_count) + 1
        });
      })
      .then(() => {
        return authorRef
          .collection('followers')
          .doc('content')
          .get();
      })
      .then(doc => {
        let data = doc.data();
        if (typeof data.uids != 'undefined') {
          followers = data.uids;

          let batch = db.batch();

          followers.forEach(fol => {
            const ref = feedRef.doc();
            batch.set(ref, {
              createdAt: admin.firestore.FieldValue.serverTimestamp(),
              uid: fol,
              creatorUid: uid,
              postId: context.params.postID,
              isResharedPost: false,
              wasViewed: false,
              wasReshared: false,
              wasLiked: false,
              wasDirectlyShared: false
            });
          });

          // Commit the batch
          return batch.commit();
        } else {
          return null;
        }
      });
  });
票数 1
EN

Stack Overflow用户

发布于 2019-04-19 16:15:40

此外,您应该在这里研究如何使用transactions.batched编写。这允许您定义一系列的读/写,然后同时执行它们。此方法可能比较慢的部分原因是您正在执行多个读/写(在处理过程中出现故障时,这也是不好的)。https://firebase.google.com/docs/firestore/manage-data/transactions#transactions

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55763709

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档