【问题标题】:Android : Notified when all Async calls are completedAndroid:当所有异步调用完成时通知
【发布时间】:2018-05-07 00:05:18
【问题描述】:

我被困在一个实现中。我正在开发一个 Android 应用程序,我在其中集成了 3rd 方 SDK(库)并调用它的 API。 SDK的函数调用实际上是异步调用(库调用的WebAPI调用),它给出响应(成功或失败)。

现在,我试着用代码来解释一下情况。

for (................) {
    AsyncAPICall (SuccessListener { 
        onSuccess() {
            for (................) {
                AsyncAPICall (SuccessListener { 
                    onSuccess() {
                        for (................) {
                            AsyncAPICall (SuccessListener, ErrorListener);
                        }
                    }
                }, ErrorListener);
            }
        }
    }, ErrorListener);
}

我想要通知或回调,或通过某事通知所有异步调用已完成。异步调用在不同的线程中运行,由于 for 循环,许多调用将同时完成。

是否有任何机制可以在所有异步调用收到响应或我需要手动执行某些操作时通知我?

有什么帮助吗?

【问题讨论】:

    标签: java android asynchronous observer-pattern greenrobot-eventbus


    【解决方案1】:

    @Khushbu Shah,我更新了我的答案,它改变了一点:)(很长)。
    为了确保它正常工作,我创建了一个独立的工作示例并使用此 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);
        }
    

    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
    }
    

    app/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;
        }
    }
    

    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);
    }
    

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

    【讨论】:

    • 我同意 rxjava 在这里会很好,但你的例子几乎没有意义,至少预期 zip() observeOn() 和 subscribeOn()
    • 感谢“@Think Twise Code Once”。首先我了解了 RxJava,然后我会尝试这个解决方案。
    • @TimCastelijns 在给出这个答案之前我看了一下 Observable.zip() ,但我认为这个解决方案更简单。
    • 如果你认为这是一个很好的解决方案,你不知道 rxjava 是如何工作的
    • 也许你是对的,在这种情况下我会尝试更深入,我希望你能给出更好的答案。
    猜你喜欢
    • 2016-12-14
    • 2019-09-14
    • 1970-01-01
    • 2016-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-04
    • 2016-02-03
    相关资源
    最近更新 更多