diff --git a/SDWebImageSwiftUI/Classes/AnimatedImage.swift b/SDWebImageSwiftUI/Classes/AnimatedImage.swift index 4e5a7a75..b64806d8 100644 --- a/SDWebImageSwiftUI/Classes/AnimatedImage.swift +++ b/SDWebImageSwiftUI/Classes/AnimatedImage.swift @@ -262,8 +262,8 @@ public struct AnimatedImage : PlatformViewRepresentable { } DispatchQueue.main.async { context.coordinator.imageLoading.progress = progress + self.imageHandler.progressBlock?(receivedSize, expectedSize) } - self.imageHandler.progressBlock?(receivedSize, expectedSize) }) { (image, data, error, cacheType, finished, _) in context.coordinator.imageLoading.image = image context.coordinator.imageLoading.isLoading = false diff --git a/SDWebImageSwiftUI/Classes/ImageManager.swift b/SDWebImageSwiftUI/Classes/ImageManager.swift index 045bfa37..94c20c86 100644 --- a/SDWebImageSwiftUI/Classes/ImageManager.swift +++ b/SDWebImageSwiftUI/Classes/ImageManager.swift @@ -86,20 +86,18 @@ public final class ImageManager : ObservableObject { 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 - } let progress: Double if (expectedSize > 0) { progress = Double(receivedSize) / Double(expectedSize) } else { progress = 0 } - self.indicatorStatus.progress = progress - if let progressBlock = self.progressBlock { - DispatchQueue.main.async { - progressBlock(receivedSize, expectedSize) + DispatchQueue.main.async { [weak self] in + guard let self = self else { + return } + self.indicatorStatus.progress = progress + self.progressBlock?(receivedSize, expectedSize) } }) { [weak self] (image, data, error, cacheType, finished, _) in guard let self = self else { diff --git a/Tests/ImageManagerTests.swift b/Tests/ImageManagerTests.swift index 8222f5b2..79a77867 100644 --- a/Tests/ImageManagerTests.swift +++ b/Tests/ImageManagerTests.swift @@ -40,4 +40,37 @@ class ImageManagerTests: XCTestCase { sub.cancel() self.waitForExpectations(timeout: 10, handler: nil) } + + func testImageManagerProgressDispatchesToMainThread() throws { + let expectation = self.expectation(description: "ImageManager progress on main thread") + let imageUrl = URL(string: "https://example.com/test.png") + let imageManager = ImageManager() + + class MockLoader: NSObject, SDImageLoader { + func canRequestImage(for url: URL?) -> Bool { true } + func requestImage(with url: URL?, options: SDWebImageOptions = [], context: [SDWebImageContextOption : Any]?, progress: SDImageLoaderProgressBlock?, completed: SDImageLoaderCompletedBlock? = nil) -> SDWebImageOperation? { + DispatchQueue.global(qos: .userInitiated).async { + progress?(50, 100, url) + DispatchQueue.global(qos: .userInitiated).async { + completed?(nil, nil, nil, true) + } + } + return nil + } + func shouldBlockFailedURL(with url: URL, error: Error) -> Bool { false } + } + + let mockManager = SDWebImageManager(cache: SDImageCache.shared, loader: MockLoader()) + + imageManager.setOnProgress { receivedSize, expectedSize in + XCTAssertTrue(Thread.isMainThread) + XCTAssertEqual(imageManager.indicatorStatus.progress, 0.5) + XCTAssertEqual(receivedSize, 50) + XCTAssertEqual(expectedSize, 100) + expectation.fulfill() + } + + imageManager.load(url: imageUrl, context: [.customManager: mockManager]) + self.waitForExpectations(timeout: 5, handler: nil) + } }