Firebase有时连接,有时不连接(React Native) [英] Firebase sometimes connecting, sometimes not connecting (React Native)

查看:140
本文介绍了Firebase有时连接,有时不连接(React Native)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常混乱的问题。我一直在使用Firebase和React Native,它一直运行良好,但最近有时当我启动应用程序连接,有时当我做不到。我想这可能与我连接数据库的方式有关。我在一个简单的JavaScript文件中做这件事,然后当我想引用一个数据库的时候,我在整个应用程序的不同视图中调用它。我不确定如何做到这一点,而不经常重新初始化,所以我想我可以做一次,并将其存储在一个变量。



这是我使用的代码初始化..没有什么特别的..完全是什么说使用(显然删除了这个帖子的关键文件的东西):

  console.log(这也是我们所有初始化的东西都是的地方)

//连接的东西
const firebase = require(firebase);

$ b常量firebaseConfig = {
apiKey:,
authDomain:,
databaseURL:,
storageBucket: ,
};
常量firebaseObject = firebase.initializeApp(firebaseConfig);
console.log(=== Firebase Intialized ===);
// TODO - 删除我
firebaseObject.database()。ref()。child('testConnect')。push({title:Summer2016});


module.exports = {
firebaseObject
}

我推送一个对象来验证我的连接,有时它会被添加到数据库中。有时它不是。我也在整个应用程序中进行数据库引用。哪些有时工作,有时不会

我怀疑这是与本机反应或我试图在多个文件中使用相同的对象的方式..但是我真的不知道该怎么办。



感谢您的帮助! 我遇到了同样的问题,似乎配置不应该在模块级别处理,而应该在应用程序准备就绪后执行。您可以在稍后手动触发的方法中设置配置。因为记住调用这个方法可能会很繁琐,所以我想出了一个包装到数据库 ref的小程序,当我们第一次调用 firebase.database()构造函数:

database.js

  import * as firebase from'firebase'

let HAS_INITIALIZED = false
$ b $常量initFirebase = ()=> {
if(!HAS_INITIALIZED){
const config = {
apiKey:**************,
authDomain:*信息信息亦读目的信息信息范范信范上信息b

$ b Firebase.database.enableLogging(true)
Firebase.initializeApp(config)
HAS_INITIALIZED = true



export const getDatabase =()=> {
initFirebase()
return Firebase.database()
}



<然后,在 whatever-file-you-want.js

  import {getDatabase}从'./database'

getDatabase()。ref('...')//等等


I have a very confusing problem. I have been using Firebase with React Native, which has been working well, but lately sometimes when I start the app it connects, sometimes when I do it does not..

I think it might have to do with the way I connect to the database. I do it in a simple javascript file, that I then call throughout the app on its different views when I want to reference a database. I wasn't really sure how to do this without constantly reinitilizing, so I figured I could do it once and store it in a variable.

Here is the code I use to initialize.. nothing special..exactly what the say to use(obviously removed the key file stuff for the sake of this post):

console.log("Global Variable File!")
console.log("This is also where all of our intialization stuff is")

//Connection Stuff
const firebase = require("firebase");


 const firebaseConfig = {
    apiKey: "",
    authDomain: "",
    databaseURL: "",
    storageBucket: "",
  };
const firebaseObject = firebase.initializeApp(firebaseConfig);
console.log("===Firebase Intialized===");
//TODO - Remove me
firebaseObject.database().ref().child('testConnect').push({ title: "Summer2016" });


module.exports = {
  firebaseObject
}

I push a object just to verify my connection, sometimes it gets added to the database. Sometimes it dosen't. I also make database references throughout the app. Which sometimes work and sometimes don't

I suspect this is something to do with react native or the way I am trying to use the same object throughout multiple files.. but am not really sure what to do.

Thanks for the help!

解决方案

I'm encountering the same issue, it seems that the configuration should not be handled at the module level, but instead be executed once the app is ready. You can set the configuration in a method that you manually triggers later on. Since it can be tedious to remember to call this method, I came up with a little wrapper to the database ref to automatically init the app the first time we made a call to the firebase.database() constructor :

database.js

import * as Firebase from 'firebase'

let HAS_INITIALIZED = false

const initFirebase = () => {
    if (!HAS_INITIALIZED) {
        const config = {
            apiKey: "**************",
            authDomain: "************",
            databaseURL: "*********",
            storageBucket: "*********",
        }

        Firebase.database.enableLogging(true)
        Firebase.initializeApp(config)
        HAS_INITIALIZED = true
    }
}

export const getDatabase = () => {
    initFirebase()
    return Firebase.database()
}

then, in whatever-file-you-want.js

import { getDatabase } from './database'

getDatabase().ref('...') // etc.

这篇关于Firebase有时连接,有时不连接(React Native)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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