-
-
Save sfeatherstone/8cbd68d7ffee0181c1d6caa9ebc354c4 to your computer and use it in GitHub Desktop.
Parallel execution using RxJava
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun performCheck(context: Context, callback: Callback, defaultEntryPoint: Boolean = false) { | |
compositeDisposable.add(job(1).subscribeOn(Schedulers.io()) | |
.mergeWith(job(2).subscribeOn(Schedulers.io())) | |
.mergeWith(job(3).subscribeOn(Schedulers.io())) | |
.observeOn(AndroidSchedulers.mainThread()) | |
.subscribe( | |
{ callback.pass() | |
Timber.d("Job OnComplete") | |
}, | |
{ callback.fail(Callback.FailReason.LoggedOut) | |
Timber.d("Job Fail") | |
} | |
)) | |
// As list | |
val list = listOf(job(1).subscribeOn(Schedulers.io()), job(2).subscribeOn(Schedulers.io()), job(3).subscribeOn(Schedulers.io())) | |
compositeDisposable.add(Completable.merge(list) | |
.observeOn(AndroidSchedulers.mainThread()) | |
.subscribe( | |
{ callback.pass() | |
Timber.d("Job OnComplete") | |
}, | |
{ callback.fail(Callback.FailReason.LoggedOut) | |
Timber.d("Job Fail") | |
} | |
)) | |
} | |
fun job(num: Int) : Completable { | |
return Completable.fromRunnable { | |
Timber.d("->Job$num ${Thread.currentThread().name}") | |
var z = 0L | |
for (x in 0L..100000000L) { | |
z += x | |
} | |
Timber.d("Job$num $z") | |
if ((0..10).random() > 5) { | |
Timber.e("Job$num failed") | |
throw Throwable("Job$num failed") | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment