如何转换LiveData< List< Foo>>进入LiveData< List< Bar>>吗? [英] How to convert LiveData<List<Foo>> into LiveData<List<Bar>>?

查看:327
本文介绍了如何转换LiveData< List< Foo>>进入LiveData< List< Bar>>吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我们有两个LiveData对象:

Let's say we have two objects of LiveData:

LiveData<List<Foo>> fooList;
LiveData<List<Bar>> barList;

通过某种方法(或构造函数),可以将Foo转换为Bar对象. 将第一个可观察对象(具有Foo对象的列表)转换为具有Bar对象列表的可观察对象的最佳方法是什么.

And by some method (or the constructor) Foo can be converted to the Bar object. What is the best way to convert the first observable, which has the list of Foo objects to the observable with the list of Bar objects.

我知道可以这样做:

barList = Transformations.map(fooList, fooList1 -> {
        List<Bar> barList = new ArrayList<>();
        for (Foo foo: fooList1) {
            barList.add(new Bar(foo)); 
        }
        return barList;
    });

但是还有没有更好的方法类似于RxJava中的flatMap运算符,通过该方法我们可以使用列表中的项目即时进行所有必要的转换,而不是像上面的示例那样处理列表本身?

But isn't there a better way similar to the flatMap operator in RxJava, by which we make all of the necessary conversions with the items from the list on the fly instead of dealing with the lists themselves as in the example above?

推荐答案

您可以按照源代码中显示的配方Transformations创建自己的转换. FWIW,此示例应用演示了自定义实施:

You can create your own transformation, following the recipe shown in the source to Transformations. FWIW, this sample app demonstrates a custom filter() implementation:

/***
 Copyright (c) 2017 CommonsWare, LLC
 Licensed under the Apache License, Version 2.0 (the "License"); you may not
 use this file except in compliance with the License. You may obtain a copy
 of the License at http://www.apache.org/licenses/LICENSE-2.0. Unless required
 by applicable law or agreed to in writing, software distributed under the
 License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
 OF ANY KIND, either express or implied. See the License for the specific
 language governing permissions and limitations under the License.

 Covered in detail in the book _Android's Architecture Components_
 https://commonsware.com/AndroidArch
 */

package com.commonsware.android.livedata;

import android.arch.lifecycle.LiveData;
import android.arch.lifecycle.MediatorLiveData;
import android.arch.lifecycle.Observer;
import android.support.annotation.MainThread;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

class LiveTransmogrifiers {
  interface Confirmer<T> {
    boolean test(T thingy);
  }

  @MainThread
  static <X> LiveData<X> filter(@NonNull LiveData<X> source,
                                @NonNull final Confirmer<X> confirmer) {
    final MediatorLiveData<X> result=new MediatorLiveData<>();

    result.addSource(source, new Observer<X>() {
      @Override
      public void onChanged(@Nullable X x) {
        if (confirmer.test(x)) {
          result.setValue(x);
        }
      }
    });

    return(result);
  }
}

否则,不可以. LiveData是作为生命周期感知的轻量级RxJava类似物.如果需要大量运算符,则最好直接使用RxJava.

Otherwise, no. LiveData is meant as a lightweight RxJava analogue that is lifecycle-aware. If you need lots of operators, you are better served using RxJava directly.

这篇关于如何转换LiveData&lt; List&lt; Foo&gt;&gt;进入LiveData&lt; List&lt; Bar&gt;&gt;吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆