Transformations.switchMap()

先看源码:

/*
 * Copyright (C) 2017 The Android Open Source Project
 *
 * 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.
 */

package androidx.lifecycle;

import androidx.annotation.MainThread;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.arch.core.util.Function;

/**
 * Transformation methods for {@link LiveData}.
 * 

* These methods permit functional composition and delegation of {@link LiveData} instances. The * transformations are calculated lazily, and will run only when the returned {@link LiveData} is * observed. Lifecycle behavior is propagated from the input {@code source} {@link LiveData} to the * returned one. */ @SuppressWarnings("WeakerAccess") public class Transformations { private Transformations() { } /** * Returns a {@code LiveData} mapped from the input {@code source} {@code LiveData} by applying * {@code mapFunction} to each value set on {@code source}. *

* This method is analogous to {@link io.reactivex.Observable#map}. *

* {@code transform} will be executed on the main thread. *

* Here is an example mapping a simple {@code User} struct in a {@code LiveData} to a * {@code LiveData} containing their full name as a {@code String}. * *

     * LiveData userLiveData = ...;
     * LiveData userFullNameLiveData =
     *     Transformations.map(
     *         userLiveData,
     *         user -> user.firstName + user.lastName);
     * });
     * 
* * @param source the {@code LiveData} to map from * @param mapFunction a function to apply to each value set on {@code source} in order to set * it * on the output {@code LiveData} * @param the generic type parameter of {@code source} * @param the generic type parameter of the returned {@code LiveData} * @return a LiveData mapped from {@code source} to type {@code } by applying * {@code mapFunction} to each value set. */ @MainThread public static LiveData map( @NonNull LiveData source, @NonNull final Function mapFunction) { final MediatorLiveData result = new MediatorLiveData<>(); result.addSource(source, new Observer() { @Override public void onChanged(@Nullable X x) { result.setValue(mapFunction.apply(x)); } }); return result; } /** * Returns a {@code LiveData} mapped from the input {@code source} {@code LiveData} by applying * {@code switchMapFunction} to each value set on {@code source}. *

* The returned {@code LiveData} delegates to the most recent {@code LiveData} created by * calling {@code switchMapFunction} with the most recent value set to {@code source}, without * changing the reference. In this way, {@code switchMapFunction} can change the 'backing' * {@code LiveData} transparently to any observer registered to the {@code LiveData} returned * by {@code switchMap()}. *

* Note that when the backing {@code LiveData} is switched, no further values from the older * {@code LiveData} will be set to the output {@code LiveData}. In this way, the method is * analogous to {@link io.reactivex.Observable#switchMap}. *

* {@code switchMapFunction} will be executed on the main thread. *

* Here is an example class that holds a typed-in name of a user * {@code String} (such as from an {@code EditText}) in a {@link MutableLiveData} and * returns a {@code LiveData} containing a List of {@code User} objects for users that have * that name. It populates that {@code LiveData} by requerying a repository-pattern object * each time the typed name changes. *

* This {@code ViewModel} would permit the observing UI to update "live" as the user ID text * changes. * *

     * class UserViewModel extends AndroidViewModel {
     *     MutableLiveData nameQueryLiveData = ...
     *
     *     LiveData> getUsersWithNameLiveData() {
     *         return Transformations.switchMap(
     *             nameQueryLiveData,
     *                 name -> myDataSource.getUsersWithNameLiveData(name));
     *     }
     *
     *     void setNameQuery(String name) {
     *         this.nameQueryLiveData.setValue(name);
     *     }
     * }
     * 
* * @param source the {@code LiveData} to map from * @param switchMapFunction a function to apply to each value set on {@code source} to create a * new delegate {@code LiveData} for the returned one * @param the generic type parameter of {@code source} * @param the generic type parameter of the returned {@code LiveData} * @return a LiveData mapped from {@code source} to type {@code } by delegating * to the LiveData returned by applying {@code switchMapFunction} to each * value set */ @MainThread public static LiveData switchMap( @NonNull LiveData source, @NonNull final Function> switchMapFunction) { final MediatorLiveData result = new MediatorLiveData<>(); result.addSource(source, new Observer() { LiveData mSource; @Override public void onChanged(@Nullable X x) { LiveData newLiveData = switchMapFunction.apply(x); if (mSource == newLiveData) { return; } if (mSource != null) { result.removeSource(mSource); } mSource = newLiveData; if (mSource != null) { result.addSource(mSource, new Observer() { @Override public void onChanged(@Nullable Y y) { result.setValue(y); } }); } } }); return result; } }

我们来扩展下:输入框中输入一个字符串,然后搜索,然后调用接口或者其他,用recycleview展示出来。
先看viewModel中的代码:

 

//查询的关键字livedata
val query = MutableLiveData()
//你点击搜索是调用的方法
   fun queryByStr(str: String) = apply { query.value = str}
//你的处理方法
fun yourFun(str:String):LiveData>{
return 你网络或者数据库.query(str)
}

//查询结果livedata
val queryResult=LiveData>=Transformations.switchMap(
            query,
            ::yourFun
    )

再看activity中的代码:

 

recycleview初始化,设置adapter就不写了

搜索.setOnClickListener{
viewModel.queryByStr(搜索框.text.toString())}
viewModel.queryResult.observe(this,Observer {
//搜索结果变化了
直接给adapter的数据源list改变,然后notify一下就完事了
})

大概基本上差不多就这样了

  在map()功能 
LiveData userLiveData = ...; 
LiveData userName = Transformations.map(userLiveData, user -> { 
    return user.firstName + " " + user.lastName; // Returns String 
}); 
 
每次的userLiveData的值发生变化,userName将被更新到。请注意,我们正在返回String。 
在switchMap()功能: 
MutableLiveData userIdLiveData = ...; 
LiveData userLiveData = Transformations.switchMap(userIdLiveData, id -> 
    repository.getUserById(id)); // Returns LiveData 

void setUserId(String userId) { 
    this.userIdLiveData.setValue(userId); 

 
每次的userIdLiveData的值发生变化,repository.getUserById(id)将被调用,就像地图功能。但repository.getUserById(id)返回LiveData。所以每当repository.getUserById(id)返回的LiveData的值发生变化时,userLiveData的值也会改变。所以userLiveData的值将取决于userIdLiveData的变化以及repository.getUserById(id)的值的变化。 
switchMap()的实际示例:假设您有一个用户配置文件,其中包含一个关注按钮和一个用于设置其他配置文件信息的下一个配置文件按钮。下一个配置文件按钮将调用setUserId()与另一个ID,所以userLiveData将改变,用户界面将改变。

-----------------------------------------------------------------

一. Transformations.map
Function:从一个LiveData转换到另外一个LiveData

val oldLiveData = MutableLiveData("hhh")
val newLiveData = Transformations.map(oldLiveData){ 
        "NewLiveData $it"
}
结果newLiveData的value就是"NewLiveData hhh"
oldLiveData也可以是从repository获取的数据
OR
需要添加依赖:implementation "androidx.lifecycle:lifecycle-livedata-ktx:$lifecycle_version"
1
val oldLiveData = MutableLiveData("hhh")
val newLiveData = oldLiveData.map{ 
        "NewLiveData $it"
}
二. Transformations.switchMap
Function: 用一个LiveData的value改变来触发另外一个LiveData的获取

用法① 直接触发
var triggerLiveData = MutableLiveData()
val targetLiveData = Transformations.switchMap(triggerLiveData){
        repository.getTargetLiveData()
}
OR
var triggerLiveData = MutableLiveData()
val targetLiveData = triggerLiveData.switchMap{
        repository.getTargetLiveData()
}

当triggerLiveData的value被改变时,就触发
 repository.getTargetLiveData()
1
用法② 条件触发
var triggerLiveData = MutableLiveData()
val targetLiveData = Transformations.switchMap(triggerLiveData){
    if(it){
        repository.getTargetLiveDataFromKK()
    }else{
        repository.getTargetLiveDataFromJJ()
    }
}

根据triggerLiveData的改变的value来判断从哪个repository读取数据
 

参考:https://blog.csdn.net/qq_43709922/article/details/104283071

https://blog.csdn.net/newmandirl/article/details/100022021?depth_1-utm_source=distribute.pc_relevant.none-task&utm_source=distribute.pc_relevant.none-task

 

你可能感兴趣的:(Android开发)