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

How do you cancel all pending and current image download requests in AlamofireImage? (Possible Feature Request) #459

Open
xanderbuck opened this issue Oct 6, 2022 · 1 comment
Assignees

Comments

@xanderbuck
Copy link

Introduction:

Hello, I'm currently using AlamofireImage to download images in my app. I'm primarily using its ImageDownloader to download images and am using a singleton instance of this ImageDownloader to download images in a custom URLImageView I built in SwiftUI.

As far as I can tell, there doesn't seem to be a cancelAll method on the ImageDownloader class. The best there is, is a cancelRequest(with requestReceipt: RequestReceipt) method, but this requires keeping track of RequestsReceipts which is overhead I'd prefer not to manage. I've played around with this as well but it doesn't seem to cancel currently downloading images, only pending in the queue (from what I've found in testing)

Problem I'm trying to solve:

I'm running into an issue in my app where I have a few screens back to back that are VERY image heavy and display many images. When a user is on a slow cell service connection (not wifi), I see that theres a bottleneck that begins to happen where when a user goes to the next screen, it can't begin downloading the new images because its bogged down waiting for the previous screens images to complete downloading.

I'd love for a way to have a generic cancelAll method I can call that just cancels everything already pending/downloading since as I don't need previous screens to continue downloading - I need the new screen to have all the bandwidth priority to download new images.

Solutions I've Tried:

The closest I've come to replicate the behavior I want is with the following method:

func cancelAll() {

	// invalidate and cancel session
	alamofireImageDownloader.sessionManager.session.invalidateAndCancel()

	// re-init alamofire image downloader
	alamofireImageDownloader = ImageDownloader(
		configuration: ImageDownloader.defaultURLSessionConfiguration(),
        downloadPrioritization: .fifo,
        maximumActiveDownloads: 4,
        imageCache: cache
    )

}

This solution makes me nervous however because it feels weird to have to re-init the ImageDownloader every time I go to a new screen in order to cancel all pending and current image downloads.

That said this solution does work, and it allows new screens to get the full bandwidth to download images and is no longer blocked by previous screen images.

Would love some thoughts on best practice on how to do this or comments on whether my current solution is safe/smart to do.

@jshier
Copy link
Contributor

jshier commented Oct 6, 2022

Your workaround technically works but it skips some internal book keeping, so a native solution would be best. Feel free to put together a PR.

You really shouldn't recreate the downloader, that's rather expensive and doesn't really do much.

@jshier jshier self-assigned this Oct 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants