I am new to android and java in general.
I am trying to create an android application where I have objects stored in firebase realtime database. On demand, I fetch objects from database using addListnerForSingleValueEvent.
mDatabase.child(itemType).child(id).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
T entity = dataSnapshot.getValue(typeParameterClass);
callback.onSuccess(entity);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
callback.onFailure();
}
});
Here callback is an object of my own callback class.
I am trying to understand how to keep entity updated at all times.
is there a way in which I can retain the reference to the entity object I created and update it when object is updated in database using addValueEventListner so that it is reflected across the application? I understand that in recycler view data is updated automatically. My question is not for the case of recycler view.
if there is such a way, then how would I deal with the situation where different pieces of code create different client object for same database object? Is there a way to enforce single object per database object in client?
I hope my questions are not vague.
EDIT 1:
Based on Comment, adding minimalistic database structure. There are more structures like "messages" which are complex (contains list etc).
EDIT 2:
I understand that addValueEventListner will trigger onDataChange method for each update in database. My question is how to keep local object up to date and how enforce single local object for each database object in client. Please read the original question for a bit more detail.
from How to keep local object updated with firebase database

http://www.wizweb.in
ReplyDeleteWizweb Technology is a leading software development company custom website design, software development, SMS Provider, Bulk sms, transactional sms, promotional sms, mobile app development, Hosting Solution, seo(search engine optimization) and Digital marketing etc.