Flutter Firebase Database persistence not working
Asked Answered
T

2

7

I am using the firebase_database plugin in version 1.0.1 with flutter currently testing on android.

I access the database with a singleton.

 GlobalFBInstance._internal() {
   final firebaseInstance = FirebaseDatabase.instance;

   firebaseInstance.goOnline();
   firebaseInstance.setPersistenceEnabled(true);
   firebaseInstance.setPersistenceCacheSizeBytes(10000000);

   databaseRef = firebaseInstance.reference();
   databaseRef.keepSynced(true);

   storageRef = FirebaseStorage.instance.ref();
}

Everytime after an app restart the app needs internet to get the database. I thought with the persistence and keepsynced there is no need for internet? If I have a very bad connection(tested in the emulator and on a device) it takes forever to load a gridview containing four simple strings from the database.

When I load a datasnapshot with:

 Future<DataSnapshot> getDatabaseSnap(String location) async {
    var _newref = databaseRef.child(location);
    await _newref.keepSynced(true);
    return await _newref.once();
 }

it won't load if there the internet connection is slow.

What could be the reason for this? Is there a better way to make sure the database doesn't need a connection every time?

Thanks in advance.

Edit: When waiting for persistence I get false:

 bool ispersistant = await firebaseInstance.setPersistenceEnabled(true);
Thwart answered 21/6, 2018 at 15:48 Comment(4)
So is your problem that persistence takes long or is not loading at all?Ule
there is no persistence when I close the app or the app gets closed by the system (restart, memory management...). Every time the app closed there has to be a internetconnection or no content at allThwart
Nothing in the code you shared reads any data yet. Please update to include the minimal-yet-complete code that is needed to reproduce the problem.Plutocrat
I edited the code in where I open the snapshot but I thought it isn't necessary.Thwart
D
13

Don't use FirebaseInstance to toggle persistent storage. Use FirebaseDatabase object to hold the instance and then set it to enable.

FirebaseDatabase database;
database = FirebaseDatabase.instance;
database.setPersistenceEnabled(true);
database.setPersistenceCacheSizeBytes(10000000); // 10MB cache is enough
Divorcee answered 6/7, 2018 at 7:11 Comment(2)
This worked. Thank you. Can you explain the difference between instance and database as the main object?Thwart
I had struggled with firebase on flutter too, and then suddenly my this code worked unknowingly. Then I realise that persistent storage is for whole database not for a instance. Once it is enable you don't have to turn it on every time.Divorcee
D
0

Class Persistence Firebase database

class RealtimeDataBase{
   late FirebaseDatabase firebaseDataBase;
   late DatabaseReference databaseRef;

  RealtimeDataBase(){
    firebaseDataBase = FirebaseDatabase.instance;
    firebaseDataBase.setPersistenceEnabled(true);
    firebaseDataBase.setPersistenceCacheSizeBytes('specify any value');
    databaseRef = firebaseDataBase.ref();
  }
}
Dehydrogenase answered 2/4, 2022 at 13:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.