Skip to content

Instantly share code, notes, and snippets.

@vorobeij
Created January 26, 2018 11:43
Show Gist options
  • Select an option

  • Save vorobeij/03ff903a9ad75970844704cb7fb659e6 to your computer and use it in GitHub Desktop.

Select an option

Save vorobeij/03ff903a9ad75970844704cb7fb659e6 to your computer and use it in GitHub Desktop.
perform task for each element in list in many threads with rxjava2
// perform task for each element in list in many threads with rxjava2
fun getAppsList2(): Single<MutableList<UserApp>>? {
val pm = context.packageManager
val packages = pm.getInstalledApplications(PackageManager.GET_META_DATA)
val tasks: MutableList<ApplicationInfo> = mutableListOf()
packages.mapTo(tasks) { it }
return Observable.fromIterable(tasks)
.map { appInfo ->
Observable.fromCallable {
val name = appInfo.packageName
val label = appInfo.loadLabel(pm).toString()
UserApp(name, label)
}
}.map { task -> task.subscribeOn(Schedulers.io()) }
.flatMap { t -> t }
.toList()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment