Android ViewModelProvider.NewInstanceFactory-接收相同实例
我想重新使用ViewModel和LiveData从Firebase读取节点。这是我的代码片段Android ViewModelProvider.NewInstanceFactory-接收相同实例,android,mvvm,android-livedata,Android,Mvvm,Android Livedata,我想重新使用ViewModel和LiveData从Firebase读取节点。这是我的代码片段 FirebaseDatabaseViewModel test = ViewModelProviders.of(this, new FirebaseDatabaseViewModel.Factory(getActivity().getApplication(),"node1")).get(FirebaseDatabaseViewModel.class); LiveData<D
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);
}
});
}
FirebaseDatabaseViewModel test=ViewModelProviders.of(这是新的FirebaseDatabaseViewModel.Factory(getActivity().getApplication(),“node1”).get(FirebaseDatabaseViewModel.class);
LiveData ldTest=test.getDataSnapshotLiveData();
ldTest.observe(这个,新的观察者(){
@凌驾
已更改公共void(@Nullable DataSnapshot DataSnapshot){
Log.d(“MyTag”、“liveData.observetestdatasnapshot=“+dataSnapshot”);
}
});
FirebaseDatabaseViewModel test2=ViewModelProviders.of(这是新的FirebaseDatabaseViewModel.Factory(getActivity().getApplication(),“node2”)).get(FirebaseDatabaseViewModel.class);
LiveData ldTest2=test2.getDataSnapshotLiveData();
ldTest2.观察(这个,新的观察者(){
@凌驾
已更改公共void(@Nullable DataSnapshot DataSnapshot){
Log.d(“MyTag”、“liveData.observe TEST2 dataSnapshot=“+dataSnapshot”);
}
});
}
这是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);
}
}
公共类FirebaseDatabaseViewModel扩展了AndroidViewModel{
私有最终字符串mRef;
私有最终FirebaseQueryLiveData;
公共FirebaseDatabaseViewModel(应用程序,字符串引用){
超级(应用);
this.mRef=ref;
this.liveData=newfirebaseQueryLiveData(FirebaseDatabase.getInstance().getReference(mRef));
}
@非空
公共LiveData getDataSnapshotLiveData(){
返回liveData;
}
公共静态类工厂扩展了ViewModelProvider.NewInstanceFactory{
@非空
私有最终应用程序映射应用程序;
私有最终字符串mRef;
公共工厂(@NonNull应用程序,字符串引用){
应用=应用;
this.mRef=ref;
}
@非空
@凌驾
公共T创建(@NonNull类modelClass){
返回(T)新的FirebaseDatabaseViewModel(MapApplication,mRef);
}
}
}
这里是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());
}
}
公共类FirebaseQueryLiveData扩展LiveData{
私有最终查询;
私有最终MyValueEventListener=新的MyValueEventListener();
公共FirebaseQueryLiveData(查询){
this.query=query;
}
公共FirebaseQueryLiveData(数据库参考ref){
this.query=ref;
}
@凌驾
受保护的void onActive(){
Log.d(“MyTag”、“onActive”);
query.addValueEventListener(listener);
}
@凌驾
受保护的void onInactive(){
Log.d(“MyTag”、“onInactive”);
removeEventListener(监听器);
}
私有类MyValueEventListener实现ValueEventListener{
@凌驾
公共void onDataChange(DataSnapshot DataSnapshot){
设置值(dataSnapshot);
}
@凌驾
已取消的公共void(DatabaseError DatabaseError){
Log.e(“MyTag”,“无法侦听查询”+查询,databaseError.toException());
}
}
}
问题是从FirebaseDatabase读取相同的节点
D/MyTag:liveData.observetestdatasnapshot=dataSnapshot{key=node1,value={
D/MyTag:liveData.TEST2 dataSnapshot=dataSnapshot{key=node1,value={
第二次我期望node2默认的
ViewModelProvider
只为给定的类名保留一个ViewModel
。调用工厂的唯一时间是在没有现有的
,并根据它传入的密钥返回多个不同的<代码> LiviaTa//Cube实例,保持一个<代码> HashMap <代码>,以避免重新创建<代码> LiviaTa<代码>对象:的情况下-在您的情况下,两个调用都使用相同的类名,因此您的第二个工厂是从未使用过ViewModel
一般来说,您应该只考虑一个“代码> > VIEWSMODS
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); } }
因此,我不必为ref=node1和ref=node2两次调用ViewModel,而只需调用一次并传递String[]所有需要的节点。然后返回HashMap对吗?有代码片段吗?我想到的是你的公共类FirebaseDatabaseViewModel扩展了AndroidViewModel{ 私有HashMap mLiveDataMap=新HashMap(); 公共FirebaseDatabaseViewModel(@NonNull final Application){ 超级(应用); } 公共LiveData getDataSnapshotLiveData(字符串引用){ 如果(!mLiveDataMap.containsKey(参考)){ //我们没有此引用的现有LiveData //因此,创建一个新的 mLiveDataMap.put(参考,新FirebaseQueryLiveData( FirebaseDatabase.getInstance().getReference(ref)); } 返回mLiveDataMap.get(ref); } }
中的一个方法,它接受ViewModel
并只返回特定ref
的单个ref
。在我的答案中添加了一些我认为有效的代码。谢谢你的代码。我会尝试一下,然后继续你不知道。它就像魅力一样工作:-)谢谢!这里有一些对其他人有用的链接:LiveData