gpt4 book ai didi

android - ViewModelProvider.NewInstanceFactory - 接收相同的实例

转载 作者:行者123 更新时间:2023-11-29 19:01:58 29 4
gpt4 key购买 nike

我想重新使用 ViewModel 和 LiveData 从 Firebase 读取节点。这是我在 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);
}
});
}

这是 View 模型

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);
}
}

这是实时数据

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());
}
}

问题是从 FirebaseDatabase 读取同一个节点

D/MyTag: liveData.observe TEST dataSnapshot = DataSnapshot { key = node1, value = {.....

D/MyTag: liveData.observe TEST2 dataSnapshot = DataSnapshot { key = node1, value = {....

第二次我期待node2

最佳答案

默认ViewModelProvider只保留一个 ViewModel对于给定的类名。唯一一次你Factory被调用是在不存在 ViewModel 时- 在你的例子中,你对两个调用使用相同的类名,所以你的第二个工厂永远不会被使用。

通常,您应该考虑只使用一个 ViewModel并让它返回多个不同的 LiveData基于传递给它的 key 的实例,保持 HashMap<String,LiveData>避免重新创建 LiveData它已经拥有的对象:

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);
}
}

关于android - ViewModelProvider.NewInstanceFactory - 接收相同的实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48695736/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com