Skip to content

Commit

Permalink
Fix the data race because progress block is called in non-main queue
Browse files Browse the repository at this point in the history
This match the behavior of `progress indicator`, which only update on main queue

Note: This is different behavior compared to SDWebIamge on UIKit (progress updated in global queue)
  • Loading branch information
dreampiggy committed Nov 6, 2024
1 parent 6c27d02 commit 46407f9
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion SDWebImageSwiftUI/Classes/ImageManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ public final class ImageManager : ObservableObject {
self.indicatorStatus.isLoading = true
self.indicatorStatus.progress = 0
currentOperation = manager.loadImage(with: url, options: options, context: context, progress: { [weak self] (receivedSize, expectedSize, _) in
// This block may be called in non-main thread
guard let self = self else {
return
}
Expand All @@ -95,7 +96,11 @@ public final class ImageManager : ObservableObject {
progress = 0
}
self.indicatorStatus.progress = progress
self.progressBlock?(receivedSize, expectedSize)
if let progressBlock = self.progressBlock {
DispatchQueue.main.async {
progressBlock(receivedSize, expectedSize)
}
}
}) { [weak self] (image, data, error, cacheType, finished, _) in
guard let self = self else {
return
Expand Down

0 comments on commit 46407f9

Please sign in to comment.