Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Launch the Interceptor chain on the main thread by default. #513

Merged
merged 9 commits into from
Aug 31, 2020

Conversation

colinrtwhite
Copy link
Member

@colinrtwhite colinrtwhite commented Aug 29, 2020

This PR changes the default behaviour of the interceptor chain to be launched on the main thread by default. Why do this:

  • It allows the memory cache check to occur synchronously if the request is started from the main thread.
  • It allows Interceptors to be execute on the main thread synchronously if preferred. Interceptors can also control which requests check the memory cache on the main thread by using withContext to change the current dispatcher when calling Interceptor.Chain.proceed.
  • Synchronously checking the memory cache was the default behaviour pre-0.12.0. This realigns with that behaviour, but allows consumers to move the check off the main thread if they want maximum performance.

This PR adds two new flags to ImageLoader.Builder:

  • launchInterceptorChainOnMainThread: Enables/disables launching the interceptor chain on the main thread.
  • addLastModifiedToFileCacheKey: If the memory cache check occurs on the main thread, calling File.lastModified can cause a strict mode violation. This flag disables calling File.lastModified. See Add option to disable adding File.lastModified to the cache key. #415 for context.

Fixes: #280

@colinrtwhite colinrtwhite requested a review from Jawnnypoo August 29, 2020 22:05
@@ -15,17 +17,38 @@ import okhttp3.HttpUrl
import okhttp3.Request
import okhttp3.ResponseBody

internal class HttpUrlFetcher(private val callFactory: Call.Factory) : Fetcher<HttpUrl> {
internal class HttpUriFetcher(callFactory: Call.Factory) : HttpFetcher<Uri>(callFactory) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Restoring the optimization to call HttpUrl.get as part of Fetcher.fetch instead of part of Mapper.map as Fetcher.fetch is always called on a background thread.

@colinrtwhite colinrtwhite merged commit 8cf10b5 into master Aug 31, 2020
@colinrtwhite colinrtwhite deleted the colin/fix_synchronous branch August 31, 2020 19:14
colinrtwhite added a commit that referenced this pull request Oct 5, 2022
* Execute Interceptors synchronously by default.

* Re-add HttpFetcher optimization.

* Tweaks.

* Docs.

* Formatting.

* Docs.

* Add support for addLastModifiedToFileCacheKey flag.

* Use runBlockingTest.

* Nit.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Images inside recyclerview flash when notifyDataSetChanged() is called
2 participants