ViewModelProvider.NewInstanceFactory - 接收相同的实例

ViewModelProvider.NewInstanceFactory - receive same instance

我想重新使用 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);
        }
    });
}

这是视图模型

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

}

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

}

问题是从 FirebaseDatabase 读取相同的节点

D/MyTag: liveData.observe 测试数据快照 = DataSnapshot { key = node1, value = {.....

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

第二次我期待node2

默认 ViewModelProvider 只为给定的 class 名称保留一个 ViewModel。唯一一次你的 Factory 被调用是当没有已经存在的 ViewModel - 在你的情况下,你对两个调用使用相同的 class 名称,所以你的第二个工厂永远不会被使用.

一般来说,您应该考虑只拥有一个 ViewModel 并根据传递给它的密钥让它 return 多个不同的 LiveData 实例,保持 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);
  }
}