【问题标题】:Dealing with inner Transformations.switchMap on a viewModel处理 viewModel 上的内部 Transformations.switchMap
【发布时间】:2020-02-02 18:47:51
【问题描述】:

我正在将数据从 API 加载到适配器中,当用户单击它时,它使用 DownloadManager 下载,然后我使用广播器让我的活动知道 downloadId 和超链接(房间的唯一标识符)。

到目前为止,我一直无法弄清楚如何最好地使用相同的观察者,因为最初这将只是获取数据(没有 downloadId),然后通过 downloadId 和超链接传递到存储库。到目前为止,我已经能够从存储库作为硬编码数据成功地做到这一点。

我的视图模型:

    @Inject
    ItemViewModel(@NonNull ItemRepository itemRepository){
        items = Transformations.switchMap(query, search -> {
            if (search == null){
                return AbsentLiveData.create();
            }
//            Transformations.switchMap(downloadable, inner -> {
//               itemRepository.getDBItems(search, inner.getHyperlink(), inner.getDownloadId());
//            });
            return itemRepository.getDBItems(search, null, 0);
        });

由于我无法在不执行 switchMap 的情况下从可下载的数据中获取数据,并且我无法在 itemRepository.getDBItems 不返回的情况下获取它,所以我被卡住了。

我的直播结果:

@Override
public void onReceiveResult(int resultCode, Bundle resultData) {
    if (resultCode == DOWNLOAD_ID){
        Item i = new Item();
        i.setHyperlink(resultData.getString("hyperlink"));
        i.setDownloadId(resultData.getLong("downloadId"));
        itemViewModel.setDownloadable(i);
    }
}

【问题讨论】:

    标签: android viewmodel androidx android-livedata mutablelivedata


    【解决方案1】:

    查看了 Google 示例,并在 ViewModel 中制作了一个将其包裹起来的对象。

    我的最终结果:

    @Inject
    ItemViewModel(@NonNull ItemRepository itemRepository){
        this.itemQuery = new MutableLiveData<>();
        items = Transformations.switchMap(itemQuery, input -> {
            if (input.isEmpty()){
                return AbsentLiveData.create();
            }
            return itemRepository.getDBItems(input.query, input.hyperlink, input.downloadId);
        });
    
     @VisibleForTesting
        public void setItemQuery(String query, String hyperlink, long downloadId) {
            ItemQuery update = new ItemQuery(query,hyperlink,downloadId);
            if (Objects.equals(itemQuery.getValue(), update)) {
                return;
            }
            itemQuery.setValue(update);
        }
    
    @VisibleForTesting
        static class ItemQuery{
        public final String query;
        public final String hyperlink;
        public final long downloadId;
    
        ItemQuery(String query, String hyperlink, long downloadId) {
            this.query = query;
            this.hyperlink = hyperlink;
            this.downloadId = downloadId;
        }
    
        boolean isEmpty() {
            return query == null;
        }
    
        @Override
        public boolean equals(Object o) {
            if (this == o) {
                return true;
            }
            if (o == null || getClass() != o.getClass()) {
                return false;
            }
    
            ItemQuery  itemQuery = (ItemQuery) o;
    
            if (query != null ? !query.equals(itemQuery.query) : itemQuery.query != null) {
                return false;
            }
            return hyperlink != null ? hyperlink.equals(itemQuery.hyperlink) : itemQuery.hyperlink == null;
        }
    }
    

    似乎可以达到我的预期目的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-16
      • 1970-01-01
      • 2013-08-05
      • 1970-01-01
      • 2021-02-19
      • 2015-09-12
      相关资源
      最近更新 更多