Skip to content

Commit 0b0c57f

Browse files
authored
Merge pull request #341 from SDWebImage/bugfix/progress_block_data_race
Fix the data race because progress block is called in non-main queue
2 parents 6c27d02 + 46407f9 commit 0b0c57f

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

SDWebImageSwiftUI/Classes/ImageManager.swift

+6-1
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ public final class ImageManager : ObservableObject {
8585
self.indicatorStatus.isLoading = true
8686
self.indicatorStatus.progress = 0
8787
currentOperation = manager.loadImage(with: url, options: options, context: context, progress: { [weak self] (receivedSize, expectedSize, _) in
88+
// This block may be called in non-main thread
8889
guard let self = self else {
8990
return
9091
}
@@ -95,7 +96,11 @@ public final class ImageManager : ObservableObject {
9596
progress = 0
9697
}
9798
self.indicatorStatus.progress = progress
98-
self.progressBlock?(receivedSize, expectedSize)
99+
if let progressBlock = self.progressBlock {
100+
DispatchQueue.main.async {
101+
progressBlock(receivedSize, expectedSize)
102+
}
103+
}
99104
}) { [weak self] (image, data, error, cacheType, finished, _) in
100105
guard let self = self else {
101106
return

0 commit comments

Comments
 (0)