Skip to content

Instantly share code, notes, and snippets.

@sfeatherstone
Last active April 16, 2019 14:56
Show Gist options
  • Save sfeatherstone/8cbd68d7ffee0181c1d6caa9ebc354c4 to your computer and use it in GitHub Desktop.
Save sfeatherstone/8cbd68d7ffee0181c1d6caa9ebc354c4 to your computer and use it in GitHub Desktop.
Parallel execution using RxJava
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