Firebase.database().ref().on() fires only once with Live Reload
> react-native info
OS: Windows 10
Node: 8.9.4
npm: 6.1.0
Packages: (wanted => installed)
react: ^16.3.2 => 16.4.1
react-native: ^0.55.3 => 0.55.3
// package.json
{
"dependencies": {
"react": "^16.3.2",
"react-native": "^0.55.3",
"react-native-firebase": "^4.3.6",
"react-native-google-signin": "^1.0.0-rc5",
"react-navigation": "^2.14.2",
"react-redux": "^5.0.7",
"redux": "^4.0.0",
"redux-thunk": "^2.3.0"
}
}
// /android/app/build.gradle
dependencies {
implementation(project(':react-native-firebase')) {
transitive = false
}
implementation('com.crashlytics.sdk.android:crashlytics:2.9.3@aar') {
transitive = true
}
// RNFirebase required dependencies
implementation "com.google.firebase:firebase-core:16.0.3"
implementation "com.google.android.gms:play-services-base:15.0.1"
// RNFirebase optional dependencies
implementation "com.google.firebase:firebase-ads:15.0.1"
implementation "com.google.firebase:firebase-auth:16.0.3"
implementation "com.google.firebase:firebase-config:16.0.0"
implementation "com.google.firebase:firebase-crash:16.2.0"
implementation "com.google.firebase:firebase-database:16.0.2"
implementation "com.google.firebase:firebase-firestore:17.1.0"
implementation "com.google.firebase:firebase-functions:16.1.0"
implementation "com.google.firebase:firebase-invites:16.0.3"
implementation "com.google.firebase:firebase-storage:16.0.2"
implementation "com.google.firebase:firebase-messaging:17.3.1"
implementation "com.google.firebase:firebase-perf:16.1.0"
implementation "com.facebook.react:react-native:+"
implementation "com.android.support:appcompat-v7:27.1.1"
implementation 'com.android.support:support-annotations:27.1.1'
implementation fileTree(dir: "libs", include: ["*.jar"])
implementation "com.facebook.react:react-native:+" // From node_modules
implementation(project(":react-native-google-signin")){
exclude group: "com.google.android.gms" // very important
}
implementation 'com.google.android.gms:play-services-auth:16.0.0'
}
// Run this once to be able to run the application with BUCK
// puts all compile dependencies into folder libs for BUCK to use
task copyDownloadableDepsToLibs(type: Copy) {
from configurations.compile
into 'libs'
}
apply plugin: 'com.google.gms.google-services'
com.google.gms.googleservices.GoogleServicesPlugin.config.disableVersionCheck = true
I'm on an Android, Connected Device, with Live Reload enabled I use Firebase for Auth and Realtime Database
After Live Reload fires (each time I save a file, basically), firebase.database().ref().on() stops working.
Here is my code:
I have a Redux Action to retrieve datas from the logged user
// AppRedux.js
const reducer = (state = initialState, action) => {
switch (action.type) {
case 'setCurrentUser':
return { ...state, currentUser: action.value }
break;
// { ... }
}
}
const store = createStore(reducer, applyMiddleware(thunkMiddleware))
export { store }
const setCurrentUser = (user) => {
return {
type: "setCurrentUser",
value: user
}
}
const watchUserData = () => {
return function(dispatch) {
const { currentUser } = firebase.auth()
if (currentUser) {
console.log(currentUser.uid) // I always have the right uid, so firebase.auth() always works
const ref = firebase.database().ref('users/' + currentUser.uid)
ref.on('value', (snapshot) => {
const user = snapshot.val()
console.log(user) // I get there only once !
dispatch(setCurrentUser(user))
}, (error) => {
console.log(error)
})
}
}
}
export { setCurrentUser, watchUserData }
And I call this action in a pretty simple Component (screen)
// Main.js
// { ... other imports}
import { watchUserData } from '../redux/AppRedux'
const mapStateToProps = (state) => {
return {
currentUser: state.currentUser
}
}
const mapDispatchToProps = (dispatch) => {
return {
watchUserData: () => dispatch(watchUserData())
}
}
class Main extends React.Component {
constructor(props) {
super(props)
}
componentWillMount() {
this.props.watchUserData()
}
render() {
const { currentUser } = this.props
return (
<View>
<Text>Hello { currentUser && currentUser.firstname }!</Text>
</View>
)
}
}
export default connect(mapStateToProps, mapDispatchToProps)(Main)
This piece of code works perfectly.. only once. When Live Reload fires, it won't work anymore.
Note that firebase.auth() still works and gives me the right uid, but firebase.database().ref('users/' + currentUser.uid).on('value') won't fire anymore, even if I sign out then sign in again.
Also note that it works perfectly with Hot Reloading whereas it doesn't with Live Reloading.
I have to npm run android (which is equal to react-native run-android) for it to fire again.. until the next Live Reload.
@Ehesp I think it related to AsyncTask.execute with default THREAD_POOL_EXECUTOR, after restart js code native part not clearing created listener tasks, what you think it can be true ?
Same issue is happening with me after updating to react native 0.57.1. For me even hot reloading stops giving data from firebase.on
@barbarosh what version of RNFB are you on? Are you able to try v5 - published today: https://invertase.link/rnfb-v5
Even code push mandatory update is doing the same. So I can't do code push to my production app . Listeners are not working somehow. I tried to put log in native code. As you can see from below logs value event listeners doesn't trigger ondatachange after codepush or hot reload.
Please help.

Ok thanks, I have a rough idea now what it could be, will take a look today
Hey all, could someone locally apply the PR I just sent up and let me know the result - thanks 👍
Loving react-native-firebase and the support we provide? Please consider supporting us with any of the below:
- 👉 Back financially via Open Collective
- 👉 Follow
React Native FirebaseandInvertaseon Twitter - 👉 Star this repo on GitHub ⭐️
@Salakar It doesn't seem to have an effect on my side
// package.json
"react": "^16.5.0",
"react-native": "^0.57.0",
"react-native-firebase": "^5.0.0", // I did manually change the modified files
// build.gradle
implementation "com.google.firebase:firebase-core:16.0.3"
implementation "com.google.firebase:firebase-database:16.0.2"
@Salakar It doesn't seem to have an effect on my side
// package.json "react": "^16.5.0", "react-native": "^0.57.0", "react-native-firebase": "^5.0.0", // I did manually change the modified files// build.gradle implementation "com.google.firebase:firebase-core:16.0.3" implementation "com.google.firebase:firebase-database:16.0.2"
Daft question sorry 🙈 but just to double check, did you re-build and re-install the APK after making the changes?
Could you add some logging in the onHostDestroy method and inside the iterators
@Salakar I don't think the fix worked. After hot reloading it still doesn't gives you data. I tried to put log in onHostDestroy method, nothing is getting logged. So most probably onHostDestroy in not getting called.
To reproduce issue you will have to do Hot reload and all listeners would stop giving data.
@Salakar Could this issue be related to #1407 ? I believe it might be the same
Having the same issue here with: [email protected] [email protected] [email protected]
Same here aswell. However it seems that it doesn't matter whether I use live reload or not. Same issue with live reload disabled and reloading app manually
// package.json "react": "^16.5.0", "react-native": "^0.57.1", "react-native-firebase": "^5.0.0",
//app/build.gradle implementation "com.google.firebase:firebase-core:16.0.3" implementation "com.google.firebase:firebase-auth:16.0.3" implementation "com.google.firebase:firebase-database:16.0.2"
Same to me with (i try to patch according to #1535 but doesn works)
// package.json "react": "16.5.0", "react-native": "0.57.2", "react-native-firebase": "^5.0.0",
//app/build.gradle implementation "com.google.firebase:firebase-core:16.0.3" implementation "com.google.firebase:firebase-auth:16.0.3" implementation "com.google.firebase:firebase-database:16.0.2"
Thanks to the stack trace provided by @guidotajjan in #1601 I think I know what the cause is now will re-attempt a fix today/tomorrow 👍
I think the pr doesn't solve this problem. Things i did :
- Downloaded the pr
- gradlew clean
- run-android
Still got the same messages on android logcat :
W/MessageQueue: Handler (android.os.Handler) {12abbd8} sending message to a Handler on a dead thread java.lang.IllegalStateException: Handler (android.os.Handler) {12abbd8} sending message to a Handler on a dead thread at android.os.MessageQueue.enqueueMessage(MessageQueue.java:543) at android.os.Handler.enqueueMessage(Handler.java:631) at android.os.Handler.sendMessageAtTime(Handler.java:600) at android.os.Handler.sendMessageDelayed(Handler.java:570) at android.os.Handler.post(Handler.java:326) at com.google.firebase.database.zza.zza(com.google.firebase:firebase-database@@16.0.2:1029) at com.google.firebase.database.obfuscated.zzcd.zza(com.google.firebase:firebase-database@@16.0.2:47) at com.google.firebase.database.obfuscated.zzab.zza(com.google.firebase:firebase-database@@16.0.2:286) at com.google.firebase.database.obfuscated.zzab.zza(com.google.firebase:firebase-database@@16.0.2:346) at com.google.firebase.database.obfuscated.zzk.zza(com.google.firebase:firebase-database@@16.0.2:5782) at com.google.firebase.database.obfuscated.zze.zza(com.google.firebase:firebase-database@@16.0.2:2164) at com.google.firebase.database.obfuscated.zzn.zza(com.google.firebase:firebase-database@@16.0.2:256) at com.google.firebase.database.obfuscated.zzn.zza(com.google.firebase:firebase-database@@16.0.2:5303) at com.google.firebase.database.obfuscated.zzn$zzc$2.run(com.google.firebase:firebase-database@@16.0.2:86) at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:423) at java.util.concurrent.FutureTask.run(FutureTask.java:237) at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:154) at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:269) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588) at java.lang.Thread.run(Thread.java:818)
@FinCendikia what RN version are you on - I'm unable to reproduce this when using the PR + RN 57.x
@Salakar I'm using "react": "16.6.0-alpha.8af6728", "react-native": "0.57.1", "react-native-firebase": "^5.0.0" + PR,
Everything works fine until the reload.
I have the same problem, with the difference that I'm using Firestore
Hey @Raullg98, thanks for the heads up, I will do the same for Firestore next once I can confirm the fix for DB is working 🤞
Guys using these versions of firebase deps in my build.gradle file solved the issue.
implementation "com.google.firebase:firebase-core:16.0.3"
implementation "com.google.firebase:firebase-auth:16.0.3"
implementation "com.google.firebase:firebase-firestore:17.1.0"
implementation "com.google.firebase:firebase-functions:16.1.0"
implementation "com.google.firebase:firebase-messaging:17.3.2"
implementation 'me.leolin:ShortcutBadger:1.1.21@aar' // Support badges
implementation "com.google.firebase:firebase-database:16.0.2"
implementation "com.google.firebase:firebase-invites:16.0.3"
implementation "com.google.firebase:firebase-storage:16.0.2"
with "react-native": "^0.57.3" "react-native-firebase": "^5.0.0", "react": "^16.6.0-alpha.8af6728"