Firebase-Geofire和云功能.函数结束是否意味着不再有侦听器? [英] Firebase - Geofire and Cloud Functions. Does end of function means no more listeners?

查看:62
本文介绍了Firebase-Geofire和云功能.函数结束是否意味着不再有侦听器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的云函数的index.js文件中,我具有以下函数正文:

In my cloud functions's index.js file, I have the following function body:

exports.onSuggestionCreated = functions.firestore.document('suggestions/{userId}').onCreate(event => {

    return admin.firestore().doc(`places/settings/profile`).get().then(doc => {

        [...]

        const location = data.location

        const ref = admin.database().ref(`suggestion_locations`)
        const geoFire = new GeoFire(ref)

        var geoQuery = geoFire.query({
            center: [location.latitude, location.longitude],
            radius: 1.0
        })

        geoQuery.on("key_entered", function(key, location, distance) {
           return sendMessageTo(key, title, body)
        })
    })
})

这是在内部创建函数时触发的函数.

This is inside a function that is triggered whenever something is created.

我想知道的是,即使云功能已长期终止,每当有东西进入由GeoFire的位置和半径界定的区域时,是否都会调用"key_entered"?我得到了一些奇怪的日志,表明了这一点.

What I would to know is, is the "key_entered" called every time something enters the region delimited by location and radius of GeoFire even though the cloud function has long been terminated? I got some strange logs that indicate so.

鉴于GeoFire的异步特性,在这种情况下我该怎么办?

Given the asynchronous nature of GeoFire, what could I do in this situation?

推荐答案

GeoFire依赖

GeoFire relies on keeping active listeners on the geodata that is within range. This does not match with Cloud Functions' run-and-exit paradigm.

这些概念(将lat + lon存储在geohash中并在其上进行范围查询)可以很好地工作,但是您可能必须修改该库或注意其实现细节才能使其在您的情况下起作用.

The concepts (storing lat+lon in geohashes and running range queries on that) work fine, but you may have to modify the library or pay attention to its implementation details to make it work in your situation.

最好似乎是返回例如当前在给定区域内的所有位置.这可以通过侦听key_entered事件(如您已经做的)和 侦听ready事件来完成,该事件

The best seems to be to return for example all locations that are currently within a given area. This can be done by listening to the key_entered event (as you already do) and to the ready event, which fires after the initial key_entered calls have been received.

exports.onSuggestionCreated = functions.firestore.document('suggestions/{userId}').onCreate(event => {

  return admin.firestore().doc(`places/settings/profile`).get().then(doc => {
    [...]

    const location = data.location

    return new Promise(function(resolve, reject) {
        const ref = admin.database().ref(`suggestion_locations`)
        const geoFire = new GeoFire(ref)

        var geoQuery = geoFire.query({
            center: [location.latitude, location.longitude],
            radius: 1.0
        })

        geoQuery.on("key_entered", function(key, location, distance) {
           sendMessageTo(key, title, body)
        })
        geoQuery.on("ready", function() {
            resolve();
        });
    });
  })
})

这篇关于Firebase-Geofire和云功能.函数结束是否意味着不再有侦听器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆