我应该使用哪个调度程序来解码位图
Which scheduler should I use to decode a bitmap
我注意到在我的应用程序中加载位图需要很多时间,如果它在主线程上 运行 会导致动画延迟,所以我将它移到了后台线程。
val scheduler = ...
Observable.fromCallable {
BitmapFactory.decodeResource(resources, resourceId)
}.subscribeOn(scheduler)
.observeOn(AndroidSchedulers.mainThread()).subscribe {bitmap ->
imageView.setImageBitmap(bitmap)
}
但是,有多种调度程序可供选择。一方面位图是从磁盘加载的,所以它应该是 Schedulers.io()
,但另一方面它也是计算密集型的,所以 Schedulers.computation()
是另一个候选。位图大小约为 1 兆像素,加载时间约为 50 毫秒。
那么我如何决定为该任务选择哪个调度程序?
我会说 Schedulers.io()
,因为加载资源是一个阻塞操作,在 Schedulers 中它指出 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
."
我注意到在我的应用程序中加载位图需要很多时间,如果它在主线程上 运行 会导致动画延迟,所以我将它移到了后台线程。
val scheduler = ...
Observable.fromCallable {
BitmapFactory.decodeResource(resources, resourceId)
}.subscribeOn(scheduler)
.observeOn(AndroidSchedulers.mainThread()).subscribe {bitmap ->
imageView.setImageBitmap(bitmap)
}
但是,有多种调度程序可供选择。一方面位图是从磁盘加载的,所以它应该是 Schedulers.io()
,但另一方面它也是计算密集型的,所以 Schedulers.computation()
是另一个候选。位图大小约为 1 兆像素,加载时间约为 50 毫秒。
那么我如何决定为该任务选择哪个调度程序?
我会说 Schedulers.io()
,因为加载资源是一个阻塞操作,在 Schedulers 中它指出 Schedulers.computation()
:
If the
RxJavaPlugins.setFailOnNonBlockingScheduler(boolean)
is set totrue
, attempting to execute operators that block while running on this scheduler will throw anIllegalStateException
."