invertase / react-native-firebase

🔥 A well-tested feature-rich modular Firebase implementation for React Native. Supports both iOS & Android platforms for all Firebase services.
https://rnfirebase.io
Other
11.73k stars 2.22k forks source link

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

Open zell180 opened 1 month ago

zell180 commented 1 month ago

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 - [x] I'm using Pods and my Podfile looks like: ```ruby # N/A ``` #### `AppDelegate.m`: ```objc // 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`: ```groovy // N/A ``` #### `android/app/build.gradle`: ```groovy // N/A ``` #### `android/settings.gradle`: ```groovy // N/A ``` #### `MainApplication.java`: ```java // N/A ``` #### `AndroidManifest.xml`: ```xml ```


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 commented 1 month ago

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 commented 1 month ago

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 commented 1 month ago

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 commented 1 month ago

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 commented 1 month ago

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 commented 1 month ago

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 commented 2 weeks ago

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.