如何在Android RxJava Observable中按顺序运行2个查询?

时间:2016-01-12 11:04:49

标签: android rx-java observable subscriber retrolambda

我想运行2个异步任务,一个跟随另一个(顺序)。我读过有关ZIP或Flat的内容,但我对此并不了解......

我的目的是从本地SQLite加载数据,当它完成时,它会将查询调用到服务器(远程)。

有人可以建议我,一种实现这一目标的方法吗?

这是我正在使用的RxJava Observable骨架(单个任务):

-save

3 个答案:

答案 0 :(得分:9)

如果查询不依赖于彼此,Lukas Batteau的答案是最好的。但是,如果您需要在之前从本地SQLite查询获取数据,则运行远程查询(例如,您需要远程查询参数或标题的数据),那么您可以从本地observable然后flatmap它结合两个observable 之后你从本地查询中获取数据:

   Observable<Object> localObservable = Observable.create(...)
   localObservable.flatMap(object -> 
   {
       return Observable.zip(Observable.just(object), *create remote observable here*, 
           (localObservable, remoteObservable) -> 
           {
               *combining function*
           });
   }).subscribe(subscriber);

flatmap函数允许您将本地observable转换为local&amp;组合。通过zip功能远程可观察。重申一下,这里的优点是两个observable是顺序的,zip函数只会在两个依赖的observable运行后运行。

此外,即使底层对象具有不同的类型,zip函数也允许您组合可观察对象。在这种情况下,您提供组合功能作为第3个参数。如果基础数据是相同的类型,请用合并替换zip函数。

答案 1 :(得分:6)

执行此操作的操作员为merge,请参阅http://reactivex.io/documentation/operators/merge.html

我的方法是创建两个可观察对象,比如observableLocalobservableRemote,然后合并输出:

Observable<Object> observableLocal = Observable.create(...)
Observable<Object> observableRemote = Observable.create(...)
Observable.merge(observableLocal, observableRemote)
          .subscribe(subscriber)

如果要确保在本地之后运行远程,可以使用concat

答案 2 :(得分:4)

您可以尝试我的解决方案,有几种方法可以解决您的问题 为了确保它正常工作,我创建了一个独立的工作示例并使用此API进行测试:https://jsonplaceholder.typicode.com/posts/1

private final Retrofit retrofit = new Retrofit.Builder()
            .baseUrl("https://jsonplaceholder.typicode.com/posts/")
            .addConverterFactory(GsonConverterFactory.create())
            .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
            .build();

    private final RestPostsService restPostsService = retrofit.create(RestPostsService.class);

    private Observable<Posts> getPostById(int id) {
        return restPostsService.getPostsById(id);
    }

<强> RestPostService.java

package app.com.rxretrofit;

import retrofit2.http.GET;
import retrofit2.http.Path;
import rx.Observable;

/**
 * -> Created by Think-Twice-Code-Once on 11/26/2017.
 */

public interface RestPostsService {

    @GET("{id}")
    Observable<Posts> getPostsById(@Path("id") int id);
}

Solution1 在序列中调用多个任务时使用,以前任务的结果始终是下一个任务的输入

getPostById(1)
                .concatMap(posts1 -> {
                    //get post 1 success
                    return getPostById(posts1.getId() + 1);
                })
                .concatMap(posts2 -> {
                    //get post 2 success
                    return getPostById(posts2.getId() + 1);
                })
                .concatMap(posts3 -> {
                    //get post 3success
                    return getPostById(posts3.getId() + 1);
                })
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(finalPosts -> {
                    //get post 4 success
                    Toast.makeText(this, "Final result: " + finalPosts.getId() + " - " + finalPosts.getTitle(),
                            Toast.LENGTH_LONG).show();
                });

Solution2 在序列中调用多个任务时使用,以前任务的所有结果都是最终任务的输入(例如:上传头像后并覆盖图像,调用api以使用这些图像URL创建新用户)

Observable
                .zip(getPostById(1), getPostById(2), getPostById(3), (posts1, posts2, posts3) -> {
                    //this method defines how to zip all separate results into one
                    return posts1.getId() + posts2.getId() + posts3.getId();
                })
                .flatMap(finalPostId -> {
                    //after get all first three posts, get the final posts,
                    // the final posts-id is sum of these posts-id
                    return getPostById(finalPostId);
                })
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(finalPosts -> {
                    Toast.makeText(this, "Final posts: " + finalPosts.getId() + " - " + finalPosts.getTitle(),
                            Toast.LENGTH_SHORT).show();
                });

<强> AndroidManifest

 <uses-permission android:name="android.permission.INTERNET"/>

root build.gradle

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.3.3'
        classpath 'me.tatarka:gradle-retrolambda:3.2.0'
        classpath 'me.tatarka.retrolambda.projectlombok:lombok.ast:0.2.3.a2'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }

    // Exclude the version that the android plugin depends on.
    configurations.classpath.exclude group: 'com.android.tools.external.lombok'
}

allprojects {
    repositories {
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

应用/的build.gradle

apply plugin: 'me.tatarka.retrolambda'
apply plugin: 'com.android.application'

android {
    compileSdkVersion 26
    buildToolsVersion "26.0.1"
    defaultConfig {
        applicationId "app.com.rxretrofit"
        minSdkVersion 15
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:26.+'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    testCompile 'junit:junit:4.12'

    provided 'org.projectlombok:lombok:1.16.6'
    compile 'com.squareup.retrofit2:retrofit:2.3.0'
    compile 'com.squareup.retrofit2:converter-gson:2.3.0'
    compile 'com.squareup.retrofit2:adapter-rxjava:2.3.0'
    compile 'io.reactivex:rxandroid:1.2.1'
}

<强>模型

package app.com.rxretrofit;
import com.google.gson.annotations.SerializedName;
/**
 * -> Created by Think-Twice-Code-Once on 11/26/2017.
 */
public class Posts {
    @SerializedName("userId")
    private int userId;
    @SerializedName("id")
    private int id;
    @SerializedName("title")
    private String title;
    @SerializedName("body")
    private String body;
    public int getUserId() {
        return userId;
    }
    public void setUserId(int userId) {
        this.userId = userId;
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public String getBody() {
        return body;
    }
    public void setBody(String body) {
        this.body = body;
    }
}

顺便说一句,使用 Rx + Retrofit + Dagger + MVP模式是一个很棒的组合。