Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/firebase/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Firebase Firestore云函数数组循环-写入长延迟_Firebase_Google Cloud Firestore_Google Cloud Functions - Fatal编程技术网

Firebase Firestore云函数数组循环-写入长延迟

Firebase Firestore云函数数组循环-写入长延迟,firebase,google-cloud-firestore,google-cloud-functions,Firebase,Google Cloud Firestore,Google Cloud Functions,我有一个云函数,它循环通过一个UID数组,然后为每个UID创建一个写操作。正常写入几乎立即发生,但在写入出现之前有很长的延迟 我试过不同尺寸的阵列。只有一个或两个UID的阵列速度更快,但仍有大约5-6秒的延迟 exports.addPostings = functions.firestore .document('posts/{postID}') .onCreate((snap, context) => { const newValue = snap.data();

我有一个云函数,它循环通过一个UID数组,然后为每个UID创建一个写操作。正常写入几乎立即发生,但在写入出现之前有很长的延迟

我试过不同尺寸的阵列。只有一个或两个UID的阵列速度更快,但仍有大约5-6秒的延迟

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
                  });
                });
            });
        });
    });
});

此外,您还应该研究如何在此处使用transactions.batch写入。这允许您定义一系列读/写操作,然后同时执行它们。这个方法可能慢的部分原因是,您正在执行多个读/写(并且在处理过程中失败的情况下,这也是坏的)。p> 您应该在代码中修改以下几点:

  • 您没有返回异步方法返回的承诺,这是编写云函数代码时的关键,如官方Firebase视频系列中关于“JavaScript承诺”的3个视频中所述:
  • 你应该正确地把承诺串起来
  • 正如CuriousGeorge所指出的,应该使用批处理写入
  • 以下修改应该可以做到这一点(但未测试!):


    您是否在考虑云函数中命名为
    冷启动
    的东西?当您的函数在一段时间内未执行时,云函数会将其置于使用较少资源的模式。因此,部署后的第一次呼叫将非常缓慢。冷启动会持续多长时间?有时可达10秒。这取决于各种因素。但是在几个调用之后,你的函数应该会及时响应。好吧,那么我认为这不是问题所在。我将尝试使用批处理,看看它是否有帮助谢谢你,我会立即尝试一下!我只是想知道如果这批人写的超过500个,会发生什么?很高兴我能帮助你!如果你认为我的答案“有用且研究充分”,请参阅。谢谢如果我没有弄错的话,如果您向批处理写入添加了500多个“元素”,您将得到一个异常。你可能需要制定一个策略来处理这个案例。这个答案可能会帮助你:嘿,我试图在一个类似的函数中解决同样的问题,但是我在那里遇到了问题。你觉得你能检查一下这个问题吗
    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;
            }
          });
      });