我可以观察到基于变量查询的LiveData对象吗?

时间:2019-04-06 17:25:11

标签: sqlite android-room android-viewmodel

我的代码遇到了一个我怀疑可能是自我造成的问题。因此,我可能应该首先回答这个问题。是否可以观察基于内部联接查询和List参数的Dao返回的LiveData对象?

不幸的是,我在Stackoverflow上还没有“ 10信誉”,因此显然我无法嵌入图像。但是,这是我的ERD快照,因为它可以帮助您了解我的实体之间的联系方式:https://i.ibb.co/9YW0Vbx/Screenshot-at-2019-04-06-13-04-43.png

PrayerListFragment

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

  mTagViewModel = ViewModelProviders.of(this).get(TagViewModel.class);
  mPrayerTagViewModel = 
  ViewModelProviders.of(this).get(PrayerTagViewModel.class);
  ...
  //Update the tag list with the selected tags
  mTagViewModel.getSelectedTags().observe(this, new Observer<List<Tag>>() {
     @Override
     public void onChanged(@Nullable List<Tag> tags) {   
        if(tags.size() > 0) {      
           mPrayerTagViewModel.setTagList(tags);
        }
     }
  });

  //Observe whatever prayers the view model has to show us
  mPrayerTagViewModel.getPrayers().observe(this, new Observer<List<Prayer>>() {
     @Override
     public void onChanged(@Nullable List<Prayer> prayers) {
        mPrayersAdapter.setPrayers(prayers);
     }
  });
  ...
}

PrayerTagViewModel

...
private List<String> mTagNames = new ArrayList<>();
...
public LiveData<List<Prayer>> getPrayers() { return getPrayersForTags(mTagNames); }
...
public void setTagList(List<Tag> tags) {
   mTagNames = new ArrayList<>();
   for (Tag tag: tags) {
      mTagNames.add(tag.getName());
   }
}

ITagDAO
这将返回我很容易观察到的LiveData对象:

@Query("SELECT * FROM tag_table ORDER BY name")
LiveData<List<Tag>> getAll();

@Query("SELECT * FROM tag_table WHERE selected ORDER BY name")
LiveData<List<Tag>> getSelected();

IPrayerTagDAO
但是我在观察此问题时遇到了问题,所以我首先要确保它是有效的语法:

@Query("SELECT * FROM prayer_table " +
       "INNER JOIN prayertag_table " +
       "ON summary=fk_summary " +
       "WHERE fk_name IN (:names)")
LiveData<List<Prayer>> getPrayersForTags(final List<String> names);

如果它是有效的语法,那么我可能会在片段中丢失我的可观察对象,因为对PrayerTagViewModel中的getPrayers()的调用返回了一个新的ViewModel,即与我在片段中开始观察的ViewModel不同? >

1 个答案:

答案 0 :(得分:0)

坚持不懈得到回报!我曾暗中怀疑神秘的Transformations.switchMap可以解决我的问题,但直到更多阅读后,我才意识到如何。

PrayerTagViewModel (已修改)

...
private PrayerTagRepository mPrayerTagRepository;
private MutableLiveData<List<String>> mTags = new MutableLiveData<>();
private LiveData<List<Prayer>> mPrayers =
   Transformations.switchMap(mTags, mTags -> getPrayersForTags(mTags));

public PrayerTagViewModel(@NonNull Application application) {
   super(application);
   mPrayerTagRepository = PrayerTagRepository.getRepository(application);
}
...
public LiveData<List<Prayer>> getPrayersForTags(final List<String> names) {
   return mPrayerTagRepository.getPrayersForTags(names);
}
public LiveData<List<Prayer>> getPrayers() { return mPrayers; }
...
public void setTagList(List<Tag> tags) {
   List<String> tagNames = new ArrayList<>();
   for (Tag tag: tags)
      tagNames.add(tag.getName());
   mTags.setValue(tagNames);
}

PrayersListFragment (已修改)

...
//Update the tag list with the selected tags
mTagViewModel.getSelectedTags().observe(this, new Observer<List<Tag>>() {
   @Override
   public void onChanged(@Nullable List<Tag> tags) {
      Log.i(this.getClass().getName(),"onChanged :: Tag size = " + tags.size());
      if(tags.size() > 0)
         mPrayerTagViewModel.setTagList(tags);
   }
});

//Observe whatever prayers the view model has to show us
mPrayerTagViewModel.getPrayers().observe(this,
   prayers -> mPrayersAdapter.setPrayers(prayers));

解决方案: switchMap允许我的Fragment从ViewModel观察动态变化的LiveData(实际上是MutableLiveData)。