-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
View-independent network activity #605
Comments
Hi, @webmaster128 Yes, the problem here is related to the reusing. There is a mechanism in Kingfisher's image view extension that if the target URL is not the same as the url of currently loaded image, it will neither think the image setting would finish nor call the completion handler. Instead, it just keep trying to wait the "correct" image to be back. It is a reasonable behavior for it working with UIKit and from an end-user stand. The way of Kingfisher's is not quite the same as you thought. For your specified situation, what you really need is to hook up to the downloading process. I suggest you to subclass the Maybe some code snippet would help: (I didn't test them myself, so just for you as a reference) // MyDownloader.swift
class MyDownloader: ImageDownloader {
override func downloadImage(with url: URL, options: KingfisherOptionsInfo?, progressBlock: ImageDownloaderProgressBlock?, completionHandler: ImageDownloaderCompletionHandler?) -> RetrieveImageDownloadTask? {
NetworkActivityIndicatorManager.shared.incrementActivityCount()
return super.downloadImage(with: url, options: options, progressBlock: progressBlock) { (image, error, url, data) in
NetworkActivityIndicatorManager.shared.decrementActivityCount()
completionHandler?(image, error, url, data)
}
}
}
// In some global scope
let myDownloader = MyDownloader(name: "my_downloader")
// Using the new downloader
imageView.kf.setImage(with: url, options: [.downloader(myDownloader)], |
@onevcat I'm trying to use the solution you proposed above with my own image downloader used as the default on
However, the method Do have any suggestion how to set |
Thanks a lot for the detailed answer. Unfortunately we did choose another library and I did not get the chance to test the suggested solution. But it looks like this is of interest for other users :) From my side, this issue can be closed. I leave it open for now as long as other users need it. Feel free to close this whenever you want. |
The extension methods are using I strongly suggest to use the extension Kingfisher where Base: UIImageView {
func setImageWithMyOwnDownloader(with resource: Resource?,
placeholder: Image? = nil,
options: KingfisherOptionsInfo? = nil,
progressBlock: DownloadProgressBlock? = nil,
completionHandler: CompletionHandler? = nil) -> RetrieveImageTask
{
var options = options ?? []
options.append(.downloader(ImageDownloader.default))
return setImage(with: resource,
placeholder: placeholder,
options: options,
progressBlock: progressBlock,
completionHandler: completionHandler)
}
} Then you could use anywhere else. |
Awesome. Thanks, I will try that out. |
@onevcat I have tried that and it does not work, unfortunately. The problem seems two-fold. First, the overridden method I have sold this issue by adding another The second issue is that the |
Oops, you are right, the one actually called by extension method is not the overridable one. I think it is much clearer and is a correct direction to use a delegate to implement it! But be careful that the However, I've tried the Thanks! |
On iOS, we use a global object counting network activity jobs to show the network activity indicator of job count >= 1. The interface is as sibple as
I tried to hack these calls into Kingfisher by setting an invisible custom Indivator
Now I am using this in a View of a UICollectionView, where views are reused. It turns out, that not for all network jobs, stopAnimatingView is called. I guess this is because Views are reused before the network job ends.
This is 12x startAnimatingView and only 6x stopAnimatingView.
It makes sense of course to skip stopAnimatingView when this only changes a subview of the reused view. But as soon as you have side-effects, this causes problems.
I guess it makes no sense to further misuse the View-based indivator API. But is there a way to get delegate calls for network activity even when target views are destroyed?
The text was updated successfully, but these errors were encountered: