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

[🐛] read once return cached value and not realtime value #8070

Open
1 of 10 tasks
zell180 opened this issue Oct 20, 2024 · 7 comments
Open
1 of 10 tasks

[🐛] read once return cached value and not realtime value #8070

zell180 opened this issue Oct 20, 2024 · 7 comments
Labels
Needs Attention plugin: database Firebase Realtime Database type: bug New bug report

Comments

@zell180
Copy link

zell180 commented Oct 20, 2024

Issue

I've created this function. The function works like i expect when i install my app but after few days i can't get current realtime value of database but seems that use cached value. Uninstall the app and reinstall fix the issue.


Project Files

Javascript

dbRef.setPersistenceCacheSizeBytes(100000000); // 100MB

// Lettura semplice senza listener
/**
 * Lettura secca del path passato in input
 * @param {*} submitFunction 
 * @returns 
 */
async function simpleDatabaseRead(path, isArray){
  return dbRef
          .ref(path)
          .once('value').then(snapshot => {
            try {
              if(isArray){
                let val = snapshot.val();
                //console.log(val)
                let list = Object.keys(val).map(k =>{ return {...val[k], key: k}});
                return list
              } else {
                //console.log("----------" + isArray)
                return snapshot.val()
              }
            } catch (error) {
              return null;
            }
          });
}

package.json:

# N/A

firebase.json for react-native-firebase v6:

{
  "react-native": {
    "crashlytics_auto_collection_enabled": true,
    "crashlytics_debug_enabled": true, 
    "crashlytics_disable_auto_disabler": true, 
    "crashlytics_is_error_generation_on_js_crash_enabled": true,
    "crashlytics_javascript_exception_handler_chaining_enabled": false
  }
}

iOS

Click To Expand

ios/Podfile:

  • I'm not using Pods
  • I'm using Pods and my Podfile looks like:
# N/A

AppDelegate.m:

// N/A


Android

Click To Expand

Have you converted to AndroidX?

  • my application is an AndroidX application?
  • I am using android/gradle.settings jetifier=true for Android compatibility?
  • I am using the NPM package jetifier for react-native compatibility?

android/build.gradle:

// N/A

android/app/build.gradle:

// N/A

android/settings.gradle:

// N/A

MainApplication.java:

// N/A

AndroidManifest.xml:

<!-- N/A -->


Environment

Click To Expand

react-native info output:

 OUTPUT GOES HERE
  • Platform that you're experiencing the issue on:
    • iOS
    • Android
    • iOS but have not tested behavior on Android
    • Android but have not tested behavior on iOS
    • Both
  • react-native-firebase version you're using that has this issue:
    • e.g. 5.4.3
  • Firebase module(s) you're using that has the issue:
    • e.g. Instance ID
  • Are you using TypeScript?
    • Y/N & VERSION


@mikehardy mikehardy added the plugin: database Firebase Realtime Database label Oct 20, 2024
@mikehardy
Copy link
Collaborator

Hi there! thanks for the reproduction function - I assume since the API for cache size is only available on database that it is the database module? The reproduction is not complete so I'm unsure but that's my best guess

What versions are you on currently?
What platforms do you reproduce this on? (example: "reproduced on iOS 18 real device", or "reproduced android 14 emulator" etc)

@zell180
Copy link
Author

zell180 commented Oct 20, 2024

Yes this is our Firebase component that manage all related to firebase.

I'm on react-native 0.73.9 and iOS 17 and 18, but on Android too, real device or simulator is the same.

"@react-native-firebase/analytics": "^20.1.0",
"@react-native-firebase/app": "^20.1.0",
"@react-native-firebase/auth": "^20.1.0",
"@react-native-firebase/crashlytics": "^20.1.0",
"@react-native-firebase/database": "^20.1.0",
"@react-native-firebase/dynamic-links": "^20.1.0",
"@react-native-firebase/messaging": "^20.1.0",
"@react-native-firebase/remote-config": "^20.1.0",

@mikehardy
Copy link
Collaborator

Thanks for the extra information - I won't be looking into this personally but that will help others

I can say that this sounds like it will be pretty difficult to reproduce since it appears that it takes several days for the problem to show up?

I wonder if a workaround might be in order then, along the lines of maybe https://rnfirebase.io/reference/database/query#keepSynced or similar?

alternatively, on a periodic basis you could perhaps do a goOffline/goOnline sequence to see if that affects things, or a setPersistenceEnabled false/true cycle, or a setPersistenceCacheSizeBytes to zero then your desired size

If it were me, I'd have a screen only enabled for testers / debug and I would add buttons that allowed me to try those things, then go back and try to fetch the values you suspect are cached, to see if any of those ideas have an affect even and allow fetch of current values

I don't know if any of that will actually help, but perhaps it will, and allow you to self-help / unblock on this one

@zell180
Copy link
Author

zell180 commented Oct 20, 2024

Thanks for the extra information - I won't be looking into this personally but that will help others

I can say that this sounds like it will be pretty difficult to reproduce since it appears that it takes several days for the problem to show up?

I wonder if a workaround might be in order then, along the lines of maybe https://rnfirebase.io/reference/database/query#keepSynced or similar?

alternatively, on a periodic basis you could perhaps do a goOffline/goOnline sequence to see if that affects things, or a setPersistenceEnabled false/true cycle, or a setPersistenceCacheSizeBytes to zero then your desired size

If it were me, I'd have a screen only enabled for testers / debug and I would add buttons that allowed me to try those things, then go back and try to fetch the values you suspect are cached, to see if any of those ideas have an affect even and allow fetch of current values

I don't know if any of that will actually help, but perhaps it will, and allow you to self-help / unblock on this one

i've try something like this:

// Lettura semplice senza listener
/**
 * Lettura secca del path passato in input
 * @param {*} submitFunction 
 * @returns 
 */
async function simpleDatabaseRead(path, isArray){

  const pathRef = dbRef.ref(path);
  pathRef.keepSynced(true);

  return dbRef
          .ref(path)
          .once('value').then(snapshot => {
            try {
              if(isArray){
                let val = snapshot.val();
                //console.log(val)
                let list = Object.keys(val).map(k =>{ return {...val[k], key: k}});
                return list
              } else {
                //console.log("----------" + isArray)
                return snapshot.val()
              }
            } catch (error) {
              return null;
            } finally {
              pathRef.keepSynced(false);
            }
          });
}

but seems not affect. can you recommend me something better?
i've already try gooffline, goonline but nothing happens. Clean cache is not possible beacuse of the scope of my app

@mikehardy
Copy link
Collaborator

but seems not affect

You mentioned this takes a few days to reproduce? Surprised you can already reproduce it - or you mean you added these lines of code just now and you are still getting cached values because you did not uninstall/reinstall 🤔

These are just guesses on my part, so I'm not sure how they would actually work, but it may be that once they are out of sync it is too late for the keepSynced to actually work. But again, this may have no effect whatsoever and I'm just making a bad secondary guess on a not-useful primary guess

can you recommend me something better?

Sorry, I've already posted up all the ideas I had

@zell180
Copy link
Author

zell180 commented Oct 21, 2024

but seems not affect

You mentioned this takes a few days to reproduce? Surprised you can already reproduce it - or you mean you added these lines of code just now and you are still getting cached values because you did not uninstall/reinstall 🤔

These are just guesses on my part, so I'm not sure how they would actually work, but it may be that once they are out of sync it is too late for the keepSynced to actually work. But again, this may have no effect whatsoever and I'm just making a bad secondary guess on a not-useful primary guess

can you recommend me something better?

Sorry, I've already posted up all the ideas I had

Surprised you can already reproduce it - or you mean you added these lines of code just now and you are still getting cached values because you did not uninstall/reinstall 🤔

Yes i've add that code on my app and release an update. I got cached value and not updated value.
For now i've fixed creating a function but i think the bug is still there

@tolypash
Copy link
Contributor

tolypash commented Nov 7, 2024

hey, I think I am having a similar issue and it comes down to the fact that once it's connected, it doesnt try to reconnect.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Needs Attention plugin: database Firebase Realtime Database type: bug New bug report
Projects
None yet
Development

No branches or pull requests

3 participants