coil icon indicating copy to clipboard operation
coil copied to clipboard

Unexpected cancellation - Coil doesn't work with setRetainInstance

Open darrentaft opened this issue 2 years ago • 5 comments

When setRetainInstance is true on a fragment, coil refuses to load any images after configuration change. Logs indicate that all the requests are cancelled, as the Coroutine scope is blindly cancelled from onConfigurationChanged.

I suspect this will be marked as WillNotFix as setRetainInstance is deprecated and strongly advised against - we have it in some legacy code that I have now started to refactor as a result of this bug.

Raising here for awareness and to save anyone else potentially spending hours investigating this.

Using Coil 2.5.0

darrentaft avatar Nov 09 '23 22:11 darrentaft

Are all new requests cancelled if they're launched after the view is reattached to the activity? If you use an ImageView attached to a fragment, that fragment must be attached to an activity or the request will be cancelled. If we allowed requests to proceed on detached fragments that's likely to leak the view reference.

colinrtwhite avatar Nov 13 '23 18:11 colinrtwhite

All requests from the retained fragment are immediately cancelled from the moment the device is rotated. It never recovers - e.g. I scroll the recyclerview up and down, and the image requests are all immediately cancelled.

As far as I could see, the Scope defined in RealImageLoader is cancelled from the shutdown method, which is called from onConfigurationChanged. The scope is never recreated, so all calls to it are immediately cancelled.

darrentaft avatar Nov 14 '23 13:11 darrentaft

@darrentaft The scope in RealImageLoader is only cancelled by onConfigurationChanged if the ImageLoader is freed by the GC, which won't occur for the singleton image loader. Are you sure the scope is cancelled? Requests are also cancelled if the fragment's root View is detached, which seems more likely.

colinrtwhite avatar Nov 16 '23 10:11 colinrtwhite

It's definitely attached - I'm scrolling the RecyclerView up and down the screen, and all image requests (for elements within it) immediately report that they're cancelled. The text elements around the image update correctly.

darrentaft avatar Nov 16 '23 10:11 darrentaft

@darrentaft Can you share the bit of code using Coil in your Fragment? I'm asking for curiosity.

eneim avatar Jan 04 '24 13:01 eneim