我对kotlin和rxjava有意想不到的行为。我创建了一个扩展函数,用于使用picasso
加载图像fun Picasso.loadBitmap(url: String) : Observable<Bitmap>
= Observable.create<Bitmap> {
emitter ->
Log.d("picasso load bitmap", "me ${this}")
try {
val bitmap = load(url).centerCrop()
.resize(100, 100)
.transform(CircleTransformer())
.get()
emitter.onNext(bitmap)
emitter.onComplete()
} catch (e: IOException) {
emitter.onError(e)
}
}
我在这样的近距离(几乎同时)多次调用这个,
picasso.loadBitmap(place.image_url)
.subscribeOn(Schedulers.io())
.retryWhen { error ->
error.zipWith(Observable.range(1, 5),
BiFunction<Throwable, Int, RetryWrapper> {
t1, t2 -> RetryWrapper(t2.toLong(), t1) })
.flatMap {
if(it.delay < 4){
Log.d(TAG, "retry no. ${it.delay} for ${place.image_url}")
Observable.timer(it.delay * 5, TimeUnit.SECONDS)
} else {
Log.d(TAG, "DMD ${place.image_url}")
Observable.error { it.error }
}
}
}
.subscribe (
{ bitmap ->
markers.find { it.place.id == place.id }?.let {
it.marker.icon = IconFactory.getInstance(context).fromBitmap(bitmap)
}
},
{
Log.e(TAG, "error decoding ${place.image_url}", it)
})
我希望每次调用loadBitmap
时,它都会创建一个新的observable。但我在日志中得到了这个
09-28 11:17:00.022 31694-32276/? D/picasso load bitmap: me com.squareup.picasso.Picasso@c894e26
09-28 11:17:00.068 31694-32277/? D/picasso load bitmap: me com.squareup.picasso.Picasso@c894e26
09-28 11:17:00.069 31694-31959/? D/picasso load bitmap: me com.squareup.picasso.Picasso@c894e26
09-28 11:17:00.108 31694-32278/? D/picasso load bitmap: me com.squareup.picasso.Picasso@c894e26
09-28 11:17:00.112 31694-32251/? D/picasso load bitmap: me com.squareup.picasso.Picasso@c894e26
09-28 11:17:00.125 31694-32260/? D/picasso load bitmap: me com.squareup.picasso.Picasso@c894e26
09-28 11:17:00.162 31694-31794/? D/picasso load bitmap: me com.squareup.picasso.Picasso@c894e26
09-28 11:17:00.192 31694-32280/? D/picasso load bitmap: me com.squareup.picasso.Picasso@c894e26
09-28 11:17:00.195 31694-32279/? D/picasso load bitmap: me com.squareup.picasso.Picasso@c894e26
09-28 11:17:00.219 31694-32281/? D/picasso load bitmap: me com.squareup.picasso.Picasso@c894e26
09-28 11:17:04.828 31694-32262/? D/picasso load bitmap: me com.squareup.picasso.Picasso@c894e26
09-28 11:17:14.885 31694-31793/? D/picasso load bitmap: me com.squareup.picasso.Picasso@c894e26
09-28 11:17:29.928 31694-32269/? D/picasso load bitmap: me com.squareup.picasso.Picasso@c894e26
对于所有loadBitmap
次调用,observable都是相同的。
我需要它们有自己的可观察性,因为如果我不习惯,当retryWhen
失败时,它将不会继续下一次失败。我希望这是有道理的。
将观察值放在defer
或flatmap
内不会改变任何内容。
编辑我的代码
override fun render(state: MainState) {
map?.let { map ->
val newMarkers: MutableList<PlaceMarker> = mutableListOf()
for(place in state.places) {
var placeMarker = placeMarkers.find { it.place.id == place.id }
if(placeMarker != null && map.markers.contains(placeMarker.marker)) {
newMarkers.add(placeMarker)
placeMarkers.remove(placeMarker)
} else {
if(placeMarker != null) placeMarkers.remove(placeMarker)
val option = MarkerOptions()
option.position = LatLng(place.latitude, place.longitude)
option.snippet = place.name
placeMarker = PlaceMarker(place, map.addMarker(option))
newMarkers.add(placeMarker)
picasso.loadBitmap(place.image_url)
.subscribeOn(Schedulers.io())
.retryWhen { error ->
error.zipWith(Observable.range(1, 5),
BiFunction<Throwable, Int, RetryWrapper> {
t1, t2 -> RetryWrapper(t2.toLong(), t1) })
.flatMap {
if(it.delay < 4){
Log.d(TAG, "retry no. ${it.delay} for ${place.image_url}")
Observable.timer(it.delay * 5, TimeUnit.SECONDS)
} else {
Log.d(TAG, "DMD ${place.image_url}")
Observable.error { it.error }
}
}
}
.subscribe (
{ bitmap ->
placeMarkers.find { it.place.id == place.id }?.let {
it.marker.icon = IconFactory.getInstance(context).fromBitmap(bitmap)
bitmap.recycle()
}
},
{
Log.e(TAG, "error decoding ${place.image_url}", it)
})
}
}
placeMarkers.forEach { it.marker.remove() }
placeMarkers.clear()
placeMarkers.addAll(newMarkers)
}
}
我正在使用MVP,只是为了让你看到更宽一点。那么,那就是在VIEW内部的一个函数,渲染将在MODEL完成从服务器获取数据后触发。
答案 0 :(得分:3)
你必须要小心。
中的关键字this
Log.d("picasso load bitmap", "me ${this}")
不会定位Observable
,而是接收器类型。在您的情况下Picasso
。您可以在日志me com.squareup.picasso.Picasso@c894e26
好消息是,每次Observable
的通话都会获得loadBitmap
的新实例。您可以通过以下方式查看:
val observable = picasso.loadBitmap(place.image_url)
Log.d("observable for picasso", "$observable")
observable.subscribeOn(Schedulers.io())...
所以你看,你总是在loadBitmap
的同一个实例上调用picasso
,这就是为什么你得到同一个类的输出。但是,每次打电话loadBitmap
都会创建一个新的Observable
所以你的代码很好。