Kingfisher: Fetching images in the background mode

Created on 2 Jan 2018  Â·  7Comments  Â·  Source: onevcat/Kingfisher

Check List

Issue Description

What

Currently, there is no way to schedule fetch of several images in background mode and execute some action after this fetch is completed.

What I've tried

An obvious start would be ImagePrefetcher. Unfortunately, it can't be used in background mode as it uses completionHandler from ImageDownloader, thus making it impossible to use in background mode on iOS. Or just add open property:

var delegateQueue: OperationQueue = OperationQueue

Next, I've tried to use ImageDownloader directly to create my own BackgroundImagePrefetcher, but there is no way to DI custom delegateQueue to the URLSession used by ImageDownloader.

This was described in this issue and @onevcat created a branch with an ability to change it, but it's deleted now.

What would be the solution

Short term solution would be to just open urlSession property on ImageDownloader. This way developers could facilitate it to fetch images in the background and use current delegates to respond to those being finished.

Long term solution would be to refactor ImagePrefetcher to use ImageDownloader without completion handlers, but with the use of URLSession delegate which is the only way that Apple supports background mode at the moment.

feature in plan v5

Most helpful comment

@Ankish Currently ImagePrefetcher is basically a simple wrapper to combine downloader and cache. I definitely agree with @krodak for the long-term solution. Instead of current data task-based session, we need to create download tasks instead. It is not a trivial work and I guess we may have a good chance to implement it in a major update. (I have a plan to rewrite most of the "legacy" Swift code when Swift 5, but it will not happen too soon I guess.)

I will keep in mind of this when I find some time for the next major release!

All 7 comments

@onevcat : any possible resolution to this. This stalls the main thread and it affects the tableview scrolling.

@krodak : can you paste the temporary-fix code here, will be really helpful for me to analyze this than doing it all over again.

@ankish in the end I went with custom solution as there are few places that Kingfisher forces main queue (local cache access is one of them too...)

@krodak :
Few things I am trying to analyse:
1) If I set completion handler to nil for ImagePrefetcher - would KF still have main queue issues?
2) If I use KingfisherManager.shared.retrieveImage and set URL here - it still does things in main thread. Did you try that as well ?
3) If we use local caching then we do not get the processing for gifs and other aspects of this library. Which makes it very difficul to synchrnize across the app.

My requirement is simple, I dont care much about completion. Need to throw in urls and get cached image if present else download and show.
Surprising we cannot download an image in background !
Thanks !

@onevcat : any suggestions for prefetching without main thread stall?
I don't need completion handler, just throwing URLS should be fine.

@onevcat : Will this feature/issue be considered. It is important to prefetch in most of the apps, using another library to prefetch and store in KF would be cumbersome. An update or any direction to this feature will be of great help. Thanks!

@Ankish Currently ImagePrefetcher is basically a simple wrapper to combine downloader and cache. I definitely agree with @krodak for the long-term solution. Instead of current data task-based session, we need to create download tasks instead. It is not a trivial work and I guess we may have a good chance to implement it in a major update. (I have a plan to rewrite most of the "legacy" Swift code when Swift 5, but it will not happen too soon I guess.)

I will keep in mind of this when I find some time for the next major release!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

sadeghgoo picture sadeghgoo  Â·  4Comments

kbpontius picture kbpontius  Â·  3Comments

nickgate picture nickgate  Â·  4Comments

rainerjungwirth picture rainerjungwirth  Â·  4Comments

damirstuhec picture damirstuhec  Â·  3Comments