Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Firebase.database().ref().on() fires only once with Live Reload #1498

Closed
pistou opened this issue Sep 17, 2018 · 40 comments · Fixed by #1619
Closed

Firebase.database().ref().on() fires only once with Live Reload #1498

pistou opened this issue Sep 17, 2018 · 40 comments · Fixed by #1619
Assignees
Labels
help: needs-triage Issue needs additional investigation/triaging. platform: android plugin: database Firebase Realtime Database type: bug New bug report
Milestone

Comments

@pistou
Copy link
Contributor

pistou commented Sep 17, 2018

> 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.

@pistou pistou changed the title Firebase.database().ref().on() fires only once Firebase.database().ref().on() fires only once with Live Reload Sep 17, 2018
@Ehesp Ehesp added plugin: database Firebase Realtime Database help: needs-triage Issue needs additional investigation/triaging. labels Sep 18, 2018
@barbarosh
Copy link

@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 ?

@shashankvaibhav
Copy link

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

@Salakar Salakar added this to the v5.0.1+ milestone Sep 26, 2018
@Salakar
Copy link
Member

Salakar commented Sep 26, 2018

@barbarosh what version of RNFB are you on? Are you able to try v5 - published today: https://invertase.link/rnfb-v5

@Salakar Salakar added platform: android Workflow: Needs Review Pending feedback or review from a maintainer. labels Sep 26, 2018
@shashankvaibhav
Copy link

shashankvaibhav commented Sep 27, 2018

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.
screen shot 2018-09-27 at 1 10 25 pm
screen shot 2018-09-27 at 1 10 34 pm

@Salakar
Copy link
Member

Salakar commented Sep 27, 2018

Ok thanks, I have a rough idea now what it could be, will take a look today

@Salakar
Copy link
Member

Salakar commented Sep 27, 2018

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:

@Salakar Salakar self-assigned this Sep 27, 2018
@pistou
Copy link
Contributor Author

pistou commented Sep 27, 2018

@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
Copy link
Member

Salakar commented Sep 27, 2018

@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

@shashankvaibhav
Copy link

shashankvaibhav commented Sep 27, 2018

@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.

@armanatz
Copy link

@Salakar Could this issue be related to #1407 ? I believe it might be the same

@rvolution
Copy link

Having the same issue here with:
[email protected]
[email protected]
[email protected]

@csgonutty
Copy link

csgonutty commented Oct 3, 2018

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"

@shiftrtech
Copy link

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"

@Salakar
Copy link
Member

Salakar commented Oct 17, 2018

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 👍

@FinCendikia
Copy link

I think the pr doesn't solve this problem.
Things i did :

  1. Downloaded the pr
  2. gradlew clean
  3. 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)

@Salakar
Copy link
Member

Salakar commented Oct 22, 2018

@FinCendikia what RN version are you on - I'm unable to reproduce this when using the PR + RN 57.x

@FinCendikia
Copy link

FinCendikia commented Oct 23, 2018

@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.

@Raullg98
Copy link

I have the same problem, with the difference that I'm using Firestore

@Salakar
Copy link
Member

Salakar commented Oct 23, 2018

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 🤞

@kamicodaxe
Copy link

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"

@blitzcom
Copy link

blitzcom commented Oct 25, 2018

@ruvice117 I have the same issue even with the dependencies you listed.

I can update await firebase.database().ref().update(updates), but as soon as I trigger a live reload, it does not work anymore.

@jch28
Copy link

jch28 commented Oct 26, 2018

any fix to this problem yet? Very significant issue.

@Salakar
Copy link
Member

Salakar commented Oct 27, 2018

All, I've tested #1619 and this is definitely working as a fix for RN reload issues, you can see the test suites for reloading here: 2cf778b

If I'm missing a test case then let me know (tag me here or ping me on discord), otherwise, I think this is resolved and ready for v5.1.0.

You can give it a go now on the [email protected] release - see the rc1 release notes on GitHub for the correct Firebase Native SDK versions.

Thanks ❤️


Loving react-native-firebase and the support we provide? Please consider supporting us with any of the below:

@Salakar Salakar closed this as completed Oct 27, 2018
Salakar added a commit that referenced this issue Oct 27, 2018
 - [ANDROID] [BUGFIX] [DATABASE] - Database listeners now correctly tearing down between RN reloads. (Fixes #1498 #1611 #1609)
 - [JS] [BUGFIX] [DATABASE] - Fixed an issue where `Reference.toString()` incorrectly contains `//` instead of `/` when joining the parent and child paths.
 - [JS] [BUGFIX] [DATABASE] - Rework `.push()` behaviour to match WebSDK and correctly return a Reference instance in all scenarios. (Fixes #893 #1464 #1572)
 - [JS] [ENHANCEMENT] [UTILS] - Added a `firebase.utils().database.cleanup()` utility method which removes all database listeners.
@derek-maurer
Copy link

Hello!

Forgive me if it's a dumb question, I'm very new to React Native. To get the fix would I need to update my package.json file to look like this: "react-native-firebase": "^5.1.0-rc2"?

Also, any idea on when 5.1.0 will be out?

@Ehesp
Copy link
Member

Ehesp commented Nov 15, 2018

@derek-maurer
Copy link

@Ehesp My mistake for missing that. Thank you! :)

@olegdater
Copy link

Using 5.1.0 here and still seeing the issue.

I have problems with firebase.auth().onAuthStateChanged callbacks & live reload.
After live reload onAuthStateChanged won't fire

@Salakar
Copy link
Member

Salakar commented Dec 4, 2018

@olegwn hi what platform? Could you make an issue with the details and tag me in it - thanks

@olegdater
Copy link

@Salakar this has been fixed in 5.2.0 release, I no longer see the issue

@danwoodbury
Copy link

I am also seeing this issue on [email protected]. It loads fine the first time, as soon as i reload the app it dosnt work. If I re-install the app then it works fine until reloaded.

    implementation "com.google.android.gms:play-services-base:16.0.1"
    implementation "com.google.firebase:firebase-core:16.0.3"
    implementation "com.google.firebase:firebase-messaging:17.3.2"
    implementation "com.google.firebase:firebase-database:16.0.2"
    implementation "com.google.firebase:firebase-auth:16.0.3"

@danwoodbury
Copy link

Ignore me, I downgraded to 5.2.0 and updated the deps as of the upgrade documentation and its now working (so far anyway)

@gglee89
Copy link

gglee89 commented Feb 22, 2019

@danwoodbury @olegwn
I also downgraded mine from 5.2.2 back to 5.2.0 and it's now back to normal.

Thanks

@Joaaofiilho
Copy link

Ignore me, I downgraded to 5.2.0 and updated the deps as of the upgrade documentation and its now working (so far anyway)

This worked for me as well, thanks!

@jmeistrich
Copy link

This is still happening for me in 5.3.1. Downgrading to 5.2.0 fixed it, but will the regression be fixed for upcoming builds?

@marcoacierno
Copy link

Don't know if it's useful or not (sorry if it's not!), but it still happens for me on 5.5.6. Any update for this fix? I'm using firebase database

@Eramirez06
Copy link

Eramirez06 commented Oct 21, 2019

the problems is that the off if never called when user close app (in my case)
a estupid temporaly solution is something like this

firebase.database().ref('nodo').off();
        setTimeout(async () => {
		firebase.database().ref('nodo').on(snap => {
			//aca tu codigo
		})
	}

y try to add a await to off and use callback but didn't work

@monu11296
Copy link

I am facing the same issue with firebase-firestore v5.5.6. Please suggest any workaround. @Salakar

@mikehardy
Copy link
Collaborator

The v5.x.x branch is in maintenance mode now, if this can't be reproduced on v6 it is unlikely to gain attention, and any attention it will receive will be for that branch. I'm the one doing v5 releases and I only have time to integrate community fixes, so any fix for it will need to be proposed by someone else, then I'll shepherd it to release. The linked PRs here and/or an examination of the diffs from 5.2.0 to 5.2.2 should probably show the way

@rufat
Copy link

rufat commented Jul 23, 2020

I'm still facing it. v6.3.4

Update: Upgraded to 6.7.1 and still ref().on() fires once (snapsot.val() is null).

@mikehardy mikehardy removed the Workflow: Needs Review Pending feedback or review from a maintainer. label Nov 26, 2020
@bensaine
Copy link

bensaine commented Oct 8, 2023

I'm still getting this issue on v18.5.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help: needs-triage Issue needs additional investigation/triaging. platform: android plugin: database Firebase Realtime Database type: bug New bug report
Projects
None yet
Development

Successfully merging a pull request may close this issue.