Hello everyone junior dev here, I fetch data from server in background thread and update ui in main thread, it works but i also have separate ui image loader from url here:
extension UIImageView {
func load(url: String) {
guard let urlString = URL(string: url) else {return}
DispatchQueue.global(qos: .background).async { [weak self] in
if let data = try? Data(contentsOf: urlString) {
if let image = UIImage(data: data) {
DispatchQueue.main.async {
self?.image = image
}
}
}
}
}
}
But when open screen first image loader above works and app freezes for seconds and then main ui (uiviews) update, both ui updates in main thread. How can I make it image load in main thread do not block uiview update? I want it to be like this: Ui updates and works, then when image is uploaded it should not freeze main ui updates.
Additional info: using combine to fetch data and uikit for design.
Thanks in advance!
2
Answers
Use URLSession
I think you should create a static thread instead of calling DispatchQueue.global(qos: .background), for example:
}