In my MainActivity I'm enabling firebase persistence enabled like below,
// Imports **
private lateinit var mDatabase : FirebaseDatabase
val md = FirebaseDatabase.getInstance().setPersistenceEnabled(true)
class MainActivity: AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
// bla bla
}
}
After Initialising MainActiviy my Activity2 starts destroying MainActivity. In my Activity2 viewmodel I'm Initialising all data from server like below,
class AppMainActivityViewModel(application: Application) : AndroidViewModel(application) {
lateinit var user_data_ref : DatabaseReference
lateinit var user_data_ref_lis : ValueEventListener
init {
user = FirebaseAuth.getInstance().currentUser?.uid!! }
fun updateDashboard(){
user_data_ref = FirebaseDatabase.getInstance().getReference("Users/$user")
user_data_ref_lis = user_data_ref.addValueEventListener(object : ValueEventListener {
override fun onCancelled(p0: DatabaseError) {
}
override fun onDataChange(p0: DataSnapshot) {
userdata.value = p0.getValue(UserModel::class.java)
}
})
}
override fun onCleared() {
super.onCleared()
user_data_ref.removeEventListener(user_data_ref_lis)
}
}
If I first run my application with internet, I assume here that my listeners would have cached data offline now, then I quit my app then opened without internet, I expected here that the locally cached data would trigger onData change immediately but to my surprise onData change was never triggred. I then changed my code to keepsync true to my user_data_ref, then it worked fine in offline mode.
Every time user opens the app, a bunch of old histories are being downloaded (approximately 30 reference with 100kb for each reference,Some of the old history is never going to change at all ). This greatly increases my bandwidth consumption over period as every time fresh files are downloaded with current logic. My main goal of enabling persistence is to reduce bandwidth consumption.
Following are the bunch of question I have now,
According to document, ValueEvent listener should be enough to cache data, why is it not working in my case?
Will this keep synced true increase my bandwidth consumption as it continuously checking for updates?
Will this keep synced true runs even my activity is destroyed?
If i put keepsynced true to my database reference which has 100kb of size, if there is a child update happening inside my ref which will be 1 kb change. Will this 100 kb be downloaded next time or just that 1 kb is downloaded?