GetStream / avatarview-android

✨ Supports loading profile images with fractional styles, shapes, borders, indicators, and initials for Android.
https://getstream.github.io/avatarview-android/
Apache License 2.0
432 stars 26 forks source link

Loading Images with Fractional Style with Glide. #13

Closed Normalnick12 closed 2 years ago

Normalnick12 commented 2 years ago

Hi, there seems to be a bug when uploading multiple images with Glide. For example, calling the avatarView1.loadImage(cats.take(4)) method will result in an error:

java.lang.IllegalStateException: You can't start or clear loads in RequestListener or Target callbacks. If you're trying to start a fallback request when a load fails, use RequestBuilder#error(RequestBuilder). Otherwise consider posting your into() or clear() calls to the main thread using a Handler instead.
        at com.bumptech.glide.request.SingleRequest.assertNotCallingCallbacks(SingleRequest.java:289)
        at com.bumptech.glide.request.SingleRequest.clear(SingleRequest.java:309)
        at com.bumptech.glide.manager.RequestTracker.clearAndRemove(RequestTracker.java:72)
        at com.bumptech.glide.RequestManager.untrack(RequestManager.java:660)
        at com.bumptech.glide.RequestManager.untrackOrDelegate(RequestManager.java:628)
        at com.bumptech.glide.RequestManager.clear(RequestManager.java:624)
        at io.getstream.avatarview.glide.AvatarBitmapLoader$loadBitmaps$1$2.invoke(AvatarBitmapLoader.kt:91)
        at io.getstream.avatarview.glide.AvatarBitmapLoader$loadBitmaps$1$2.invoke(AvatarBitmapLoader.kt:90)

The error refers to this code

awaitClose {
            requestManager.clear(avatarViewTarget)
        }

Since Glide loads images on the main thread, it seems that we can fix this problem by adding launch(Dispatchers.Main) to

   launch() {
        val avatarResults: ArrayList<AvatarResult> = arrayListOf()
        val avatarResultFlow = AvatarBitmapLoader.loadBitmaps(
            requestManager = Glide.with(this@collectAndCombineBitmaps),
            data = data,
            errorPlaceholder = errorPlaceholder
        )

But not sure if that would be the right decision.

skydoves commented 2 years ago

Hi @Normalnick12! I just checked the issue. The loading process was initially designed to load on the Main dispatcher, but I guess I missed setting the context to the scope. On the other hand, the network requests will run on the IO thread, so it would be okay. Thanks for reporting an issue & suggestion and this will be fixed on our next release. Thank you!

Normalnick12 commented 2 years ago

The bug has been fixed in version 1.0.3, thanks you for the library!