Reputation: 223
I want to re-use ViewModel and LiveData for reading nodes from Firebase. This is my code in Fragment
FirebaseDatabaseViewModel test = ViewModelProviders.of(this, new FirebaseDatabaseViewModel.Factory(getActivity().getApplication(),"node1")).get(FirebaseDatabaseViewModel.class);
LiveData<DataSnapshot> ldTest = test.getDataSnapshotLiveData();
ldTest.observe(this, new Observer<DataSnapshot>() {
@Override
public void onChanged(@Nullable DataSnapshot dataSnapshot) {
Log.d("MyTag", "liveData.observe TEST dataSnapshot = " + dataSnapshot);
}
});
FirebaseDatabaseViewModel test2 = ViewModelProviders.of(this, new FirebaseDatabaseViewModel.Factory(getActivity().getApplication(),"node2")).get(FirebaseDatabaseViewModel.class);
LiveData<DataSnapshot> ldTest2 = test2.getDataSnapshotLiveData();
ldTest2.observe(this, new Observer<DataSnapshot>() {
@Override
public void onChanged(@Nullable DataSnapshot dataSnapshot) {
Log.d("MyTag", "liveData.observe TEST2 dataSnapshot = " + dataSnapshot);
}
});
}
Here is ViewModel
public class FirebaseDatabaseViewModel extends AndroidViewModel {
private final String mRef;
private final FirebaseQueryLiveData liveData;
public FirebaseDatabaseViewModel(Application application, String ref) {
super(application);
this.mRef = ref;
this.liveData = new FirebaseQueryLiveData(FirebaseDatabase.getInstance().getReference(mRef));
}
@NonNull
public LiveData<DataSnapshot> getDataSnapshotLiveData() {
return liveData;
}
public static class Factory extends ViewModelProvider.NewInstanceFactory {
@NonNull
private final Application mApplication;
private final String mRef;
public Factory(@NonNull Application application, String ref) {
mApplication = application;
this.mRef = ref;
}
@NonNull
@Override
public <T extends ViewModel> T create(@NonNull Class<T> modelClass) {
return (T) new FirebaseDatabaseViewModel(mApplication, mRef);
}
}
}
Here is LiveData
public class FirebaseQueryLiveData extends LiveData<DataSnapshot> {
private final Query query;
private final MyValueEventListener listener = new MyValueEventListener();
public FirebaseQueryLiveData(Query query) {
this.query = query;
}
public FirebaseQueryLiveData(DatabaseReference ref) {
this.query = ref;
}
@Override
protected void onActive() {
Log.d("MyTag", "onActive");
query.addValueEventListener(listener);
}
@Override
protected void onInactive() {
Log.d("MyTag", "onInactive");
query.removeEventListener(listener);
}
private class MyValueEventListener implements ValueEventListener{
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
setValue(dataSnapshot);
}
@Override
public void onCancelled(DatabaseError databaseError) {
Log.e("MyTag", "Can't listen to query " + query, databaseError.toException());
}
}
}
Problem is reading same node from FirebaseDatabase
D/MyTag: liveData.observe TEST dataSnapshot = DataSnapshot { key = node1, value = {.....
D/MyTag: liveData.observe TEST2 dataSnapshot = DataSnapshot { key = node1, value = {....
Second time I expected node2
Upvotes: 1
Views: 2077
Reputation: 200130
The default ViewModelProvider
only keeps a single ViewModel
for a given class name. The only time your Factory
is invoked is when there's no already existing ViewModel
- in your case, you're using the same class name for both calls, so your second factory is never used.
Generally, you should consider only having a single ViewModel
and have it return multiple different LiveData
instances based on the key passed into it, keeping a HashMap<String,LiveData>
to avoid recreating LiveData
objects it already has:
public class FirebaseDatabaseViewModel extends AndroidViewModel {
private HashMap<String, LiveData<DataSnapshot>> mLiveDataMap = new HashMap<>();
public FirebaseDatabaseViewModel(@NonNull final Application application) {
super(application);
}
public LiveData<DataSnapshot> getDataSnapshotLiveData(String ref) {
if (!mLiveDataMap.containsKey(ref)) {
// We don't have an existing LiveData for this ref
// so create a new one
mLiveDataMap.put(ref, new FirebaseQueryLiveData(
FirebaseDatabase.getInstance().getReference(ref)));
}
return mLiveDataMap.get(ref);
}
}
Upvotes: 2