๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๋นˆ ๊ตฌ๋ฉ ์ฑ„์šฐ๊ธฐ

[Android][Glide] You can't start or clear loads in RequestListener or Target callbacks.

์—๋Ÿฌ ๋กœ๊ทธ ํ™•์ธ

 Caused by: 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.

 

๋ฌธ์ œ์˜ ์ฝ”๋“œ

private void setData() {
    ...
    Glide.with(imageView)
            .asBitmap()
            .load(profileImageUrl)
            .into(new CustomTarget<Bitmap>(){
                @Override
                public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
                    setBitmapToImage(resource)
                }
                
                @Override
                public void onLoadCleared(@Nullable Drawable placeholder) {
                
                }
            });
    ...
}

protected void setBitmapToImage(Bitmap photo) {
    ...
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    photo.compress(Bitmap.CompressFormat.PNG, 100, strea);
    
    Glide.with(imageView)
            .load(stream.toByteArray())
            .diskCacheStrategy(DiskCacheStrategy.NONE)
            .skipMemoryCache(true)
            .circleCrop()
            .transition(DrawableTransitionOptions.withCrossFade())
            .into(imageView);
    ...
}

 

๋กœ๊ทธ๋ฅผ ๋ณด๊ณ  setBitmapToImage() ์•ˆ์— ์žˆ๋Š” Glide ์ฝ”๋“œ์— error() ์ฝ”๋“œ๋ฅผ ์‚ฝ์ž…ํ•˜๋Š” ๋“ฑ์˜ ์‹œ๋„๋ฅผ ํ•ด๋ณด์•˜์œผ๋‚˜ ๋ฌธ์ œ๊ฐ€ ํ•ด๊ฒฐ๋˜์ง€ ์•Š์•˜๋‹ค. ๐Ÿ˜•

 

ํ•ด๊ฒฐ

https://stackoverflow.com/a/52610854

 

Android, Glide 4.3.0: If you must do so, consider posting your into() or clear() calls to the main thread using a Handler instea

Android Studio 3.2, Glide 4.3.0 I need to custom handle situation when image is not load (e.g. when url is not correct or server doesn't response). Here my code Fragment: public static void

stackoverflow.com

StackOverFlow์˜ ๋‹ต๋ณ€์—์„œ ํ•ด๋‹น ์ด์Šˆ์— ๊ด€ํ•œ ๋ฌธ์„œ ๋งํฌ๋ฅผ ๊ณต์œ ํ•ด์ค˜์„œ ๋ฌธ์ œ ํ•ด๊ฒฐ์„ ์•Œ๊ฒŒ ๋๋‹ค.

https://bumptech.github.io/glide/doc/debugging.html#you-cant-start-or-clear-loads-in-requestlistener-or-target-callbacks

 

Glide v4 : Debugging

Local Logs If you have access to the device, you can look for a few log lines using adb logcat or your IDE. You can enable logging for any tag mentioned here using: adb shell setprop log.tag. VERBOSE logs tend to be more verbose but contain more useful inf

bumptech.github.io

If you attempt to start a new load in onResourceReady or onLoadFailed in a Target or RequestListener, Glide will throw an exception. We throw this exception because itโ€™s challenging for us to handle the load being recycled while itโ€™s in the middle of notifying.

๋ฐœ๋ฒˆ์—ญ

๋งŒ์•ฝ Target ์ด๋‚˜ RequestListener ์•ˆ์˜ onResourceReady ํ˜น์€ onLoadFailed ์•ˆ์— ์ƒˆ๋กœ์šด ๋กœ๋“œ๋ฅผ ์‹œ์ž‘ํ•˜๋Š” ๊ฑธ ์‹œ๋„ํ•œ๋‹ค๋ฉด, Glide๋Š” exception์„ ๋ฐœ์ƒ์‹œํ‚ฌ ๊ฒ๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๋Š” ์ด exception์„ ๋ฐœ์ƒ์‹œํ‚ฌํ…๋ฐ์š”, ์•Œ๋ฆผ ์ค‘๊ฐ„์— ์žฌํ™œ์šฉ ์ค‘์ธ ๋กœ๋“œ๋ฅผ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์–ด๋ ต๊ธฐ ๋•Œ๋ฌธ์ด์ฃ .

the middle of notifying์˜ ์˜๋ฏธ๋ฅผ ์ œ๋Œ€๋กœ ์ดํ•ดํ•˜์ง€ ๋ชปํ–ˆ๋‹ค. ๋ฌด์—‡์„ notifyํ•œ๋‹ค๋Š” ๊ฒƒ์ธ์ง€?๐Ÿ™„ ํ•˜๋‚˜์˜ ๋ฆฌ์†Œ์Šค๋ฅผ ๋‘๊ณ  ๋‘ ๊ฐœ์˜ Glide๋กœ load๋ฅผ ํ•˜๊ฒŒ๋œ๋‹ค๋ฉด, Glide ๋‚ด๋ถ€์—์„œ ๊ผฌ์ผ ์ˆ˜ ์žˆ๋Š” ํ˜„์ƒ์ด ์žˆ๋‚˜๋ณด๋‹ค, ๊ทธ๊ฒŒ load recycle๊ณผ ๊ด€๋ จ์žˆ๋‚˜ ๋ณด๋‹ค, ์ •๋„ ์ถ”๋ก ํ•˜๊ณ  ์žˆ๋‹ค.

 

setData() ์•ˆ์˜ Glide ์ฝ”๋“œ๋ฅผ ๋งŒ์ ธ์•ผ ํ•˜๋Š” ๊ฒƒ์ด์—ˆ๋‹ค. setData() ์•ˆ Glide์˜ CustomTarget์˜ onResourceReady() ์ฝœ๋ฐฑ์—์„œ ํ˜ธ์ถœํ•˜๋Š” setBitmapToImage()์ด ๋‹ค์‹œ Glide๋ฅผ ํ˜ธ์ถœํ•ด์„œ ํ•ด๋‹น ์ด์Šˆ๊ฐ€ ๋ฐœ์ƒํ•˜๋Š” ๊ฒƒ์ด์—ˆ๋‹ค.

๋ฌธ์„œ์—์„œ๋Š” onLoadFailed() ์ฝœ๋ฐฑ ์•ˆ์— Glide๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ๊ฒƒ ๋Œ€์‹ ์— RequestBuilder์˜ error()๋ฅผ ์‚ฌ์šฉํ•˜๋ผ๊ณ  ํ•˜๋Š”๋ฐ, ๋‚˜๋Š” onResourceReady()์—์„œ ํ˜ธ์ถœํ•˜๋Š” ๊ฑฐ๋ผ ๊ทธ๋ ‡๊ฒŒ ํ•  ์ˆ˜๋Š” ์—†์—ˆ๋‹ค. 4.3.0 ๋ฒ„์ „ ์ด์ „์˜ ํ•ด๊ฒฐ ๋ฐฉ์‹์„ ๊ฐ€์ ธ์™€์„œ ์ฝ”๋“œ๋ฅผ ์ˆ˜์ •ํ–ˆ๊ณ , ์ •์ƒ๋™์ž‘์„ ํ™•์ธํ–ˆ๋‹ค.

 

์ฝ”๋“œ ์ˆ˜์ •

private void setData() {
    ...
    Glide.with(imageView)
            .asBitmap()
            .load(profileImageUrl)
            .into(new CustomTarget<Bitmap>(){
                @Override
                public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
                    new Handler().post(() -> setBitmapToImage(resource));
                }
                
                @Override
                public void onLoadCleared(@Nullable Drawable placeholder) {
                
                }
            });
    ...
}