检索数据时的Firebase延迟 [英] Firebase delay when retrieving data

查看:45
本文介绍了检索数据时的Firebase延迟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Firebase API从数据库中读取数据使我感到尴尬,只是在加载Web UI之后的1-2秒突然发生了事情.任何人都可以选择读取数据吗?我已经尝试过REST API,但是我仍然需要使用Firebase authlistener,这再次给了我一定的延迟,以便我可以检索用户ID和使用REST API.我正在读取的数据有多小都没关系.

Using the Firebase API to read data from the database gives me an awkward moment of things just suddenly coming to place 1-2 seconds after web UI has loaded. Does anyone have any alternatives to reading data? I've tried REST API, but i still need to use the Firebase authlistener, which again gives me a delay before i can retrieve user id and use the REST API. It doesn't matter how small the data I'm reading is.

在加载屏幕上,我将不胜感激!

I would appreciate anything over a loading screen!

推荐答案

以下是在Firebase上获取用户状态的简单示例

below is a simple example to fetch the user status on firebase

const app = firebase.initializeApp()
const db = app.database();
let unsub;

const user = new Promise((resolve, reject) => {

    unsub = app.onAuthStateChanged(
        status => {

            if ( status === null ) {

                resolve(app.signInWithEmailAndPassword());

            } else resolve(user);

        },
        err => reject(err);
    )


});


user.then(unsub, unsub);

db.ref("path/to/data").on("child_added", () => {});

// this promise will fail if the user is not
// auth
const data = db.ref("path/to/data").once("value");

您不必等待兑现这一诺言即可打开订阅.但是,根据您的应用程序设置,如果未通过身份验证,则无法读取数据库.

You don't have to wait for this promise to be settled to open subscription. However depending of your app's settings your database cannot be read if you are not authenticated.

所以解决方案可能很简单.

So a solution can be pretty straighforward.

经过一些重构,我们可以:

With a little bit of refactor we have :

const getData = () => db.ref("path/to/data").once("value");

user.then(getData).then(data => { /* data here */ });

仅当一切正常后,用户一次身份验证,此最后一个诺言便会得到有效解决.您可以在对用户进行身份验证之前打开订阅,但不能保证订阅进行得更快.克服它的唯一方法是简单地删除首先要访问的数据库所需的读取权限...

This last promise will effectively be resolved as soon as the user is auth once if and only if everything went ok. You can open the subscription before authenticating the user but it is not guarenteed to go faster. The only way to beat it is by simply removing the reading rights needed to access the db in the first place...

这篇关于检索数据时的Firebase延迟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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