What should I use? Disk persistence or keepSynced calls?
From my experience I can say that it always depends of what your app is working, and how you manage the transactions and database of your application. If for example you have an application where the user is just writing and reading data but he is not able to delete or edit it, use DiskPersistence
would be the right choice.
Also, DiskPersistence
will store data in cache, which means that your app will use more space in the user's devices, which maybe is not the best idea in your case.
In other hand, if your application manages a lot of complex transactions and your data is updated really often, possibly you should avoid DiskPersistence
and use keepSynced
in the references that you want to keep fresh.
Why?
DiskPersistence
stores the data retrieved in local, which sometimes can cause lot of desynchronization showing your data if you don't use it together with continous ListenerValueEvents
. For example:
To avoid this the best idea is keep continous listeners in the references that you want to track all time.
Can I use both together?
Of course you can, and in most of the apps possibly is the best idea to avoid download a lot of data and give the user the possibility to work with your app even if he has no connection.
If you don't care about use cache space in the user device, I recommend you to enable diskPersistence
in your FirebaseDatabase
object and also add a keepSync
flags to each reference that can have a lot of times in a short space time or you want to keep fresh all time.
To enable disk persistence you should enable the flag persistenceEnabled
in the FirebaseDatabaseInstance
object of your application:
Android
FirebaseDatabase.getInstance().setPersistenceEnabled(true);
iOS
Database.database().isPersistenceEnabled = true //Swift
[FIRDatabase database].persistenceEnabled = YES; //Objetive-C
If you want to disable the persistence in some moment of your app lifecycle you should remember to disable it in the same way:
Android
FirebaseDatabase.getInstance().setPersistenceEnabled(false);
iOS
Database.database().isPersistenceEnabled = false //Swift
[FIRDatabase database].persistenceEnabled = NO; //Objetive-C
Firebase synchronizes and stores a local copy of the data for active listeners when used on mobile devices. In addition, you can keep specific locations in sync.
Android :
DatabaseReference workoutsRef = FirebaseDatabase.getInstance().getReference("workouts");
scoresRef.keepSynced(true);
iOs:
//Objetive-c
FIRDatabaseReference *scoresRef = [[FIRDatabase database] referenceWithPath:@"scores"];
[scoresRef keepSynced:YES];
//Swift
let scoresRef = Database.database().reference(withPath: "scores")
scoresRef.keepSynced(true)
Firebase client automatically downloads the data at these locations and keeps it updated even if the reference has no active listeners. You disable synchronization with the following line of code.
Android :
scoresRef.keepSynced(false);
iOS:
[scoresRef keepSynced:NO]; //Objetive-C
scoresRef.keepSynced(false) //Swift