【问题标题】:Firebase 3.3 realtime database stuck to "Making a connection attempt" with React Native 0.32Firebase 3.3 实时数据库坚持使用 React Native 0.32 “进行连接尝试”
【发布时间】:2023-04-05 19:37:01
【问题描述】:

firebase@3.3.0

react-native v0.32在有wifi的安卓设备上测试

Firebase 数据库没有任何身份验证规则,它是开放式读写的。

给定以下文件结构:

|_ firebase.js
|_ actions.js

这不起作用:

firebase.js

import firebase from 'firebase'

const config = {
    apiKey: "*****",
    authDomain: "****",
    databaseURL: "*****",
    storageBucket: "*****",
}

firebase.database.enableLogging(true);

export default firebase.initializeApp(config)

actions.js

import firebase from './firebase'

export const fetchData = () => {
    const Data = firebase.database().ref('some/data')
    Data.on('value', (snapshot) => {
        console.log("snapshot", snapshot.val())  // never printed
    }, (error) => {
        console.error(error)
    })
}

调试输出

p:0: Browser went online.  
firebase-database.js:36 p:0: Listen called for /some/data default  
firebase-database.js:36 p:0: Making a connection attempt

没有别的……


这确实有效(但不是解决方案)

firebase.js

...same content as above...

export default () => firebase.initializeApp(config)  // we export a function instead to trigger the initialization when the app is ready

actions.js

...same content as above...
const Data = firebase().database().ref('some/data') // we "manually" trigger the initialization, it's obviously not a good solution since we can't initialize the app multiple times

输出

p:0: Browser went online.  
firebase-database.js:36 p:0: Listen called for /some/data default  
firebase-database.js:36 p:0: Making a connection attempt  
firebase-database.js:36 p:0: Auth token refreshed  
firebase-database.js:36 getToken() completed. Creating connection. 
firebase-database.js:36 c:0:0: Connection created  

我在这里做错了什么?我还注意到,一旦我import firebase from 'firebase'firebase 变量在导入语句中不是firebase var 的所有文件中全局可用(我可以写import FooBar from 'firebase'firebase 全局变量是仍然进口)

【问题讨论】:

  • 你试过关闭调试模式吗?我有类似的问题,但它只发生在调试模式。
  • 我确实打开了调试模式,因为什么都没发生,所以很遗憾不是那样。
  • 您没有做错任何事情,最新的 firebase 和 react-native 之间出现了问题(这可能是正常的,但似乎没有在任何地方记录)。为我工作了“firebase”:“^3.1.0”,“react”:“15.2.1”,“react-native”:“^0.29.0”。
  • 好的,谢谢!不知道我应该在哪里发布问题......
  • @Pcriulan 您可以尝试不同的依赖组合,看看哪个最先中断。我实际上已经开始这样做了,但是 npm 缓存问题使得我花了很长时间才能投入到它上面。

标签: firebase react-native firebase-realtime-database react-native-android


【解决方案1】:

因为似乎没有人有“官方”的答案。这是我提供某种延迟初始化的解决方法:

firebase.js

import Firebase from 'firebase'

let _database = null

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

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

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

然后,在您需要database 的任何地方:

import { getDatabase } from './firebase'

const methodThatNeedDatabase = () => {
    getDatabase().ref('/some/ref')
    ...
}

【讨论】:

  • 这实际上是让它更快还是只是延迟延迟?
猜你喜欢
  • 1970-01-01
  • 2021-07-20
  • 2021-11-13
  • 2020-06-04
  • 1970-01-01
  • 1970-01-01
  • 2018-09-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多