Search code examples
androidmultithreadingkotlinrx-androidbitmapfactory

Which scheduler should I use to decode a bitmap


I've noticed that loading of bitmap in my appplication takes a lot of time, causing lags in animation if it is run on the main thread, so I moved it to a background thread.

val scheduler = ...
Observable.fromCallable {
    BitmapFactory.decodeResource(resources, resourceId)
}.subscribeOn(scheduler)
        .observeOn(AndroidSchedulers.mainThread()).subscribe {bitmap ->
    imageView.setImageBitmap(bitmap)
}

However, there are several schedulers to choose from. On the one hand the bitmap is loaded from disk, so it should be Schedulers.io(), but on the other hand it is also computation intensive, so Schedulers.computation() is another candidate. The bitmap has size of about 1 megapixel, and takes about 50 milliseconds to load.

So how can I decide, which scheduler to choose for this task?


Solution

  • I would say Schedulers.io(), as loading the resource is a blocking operation, and in Schedulers it states that for Schedulers.computation():

    If the RxJavaPlugins.setFailOnNonBlockingScheduler(boolean) is set to true, attempting to execute operators that block while running on this scheduler will throw an IllegalStateException."