- Notifications
You must be signed in to change notification settings - Fork256
Fix Race conditions#359
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
base:master
Are you sure you want to change the base?
Changes fromall commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -61,9 +61,27 @@ public final class ImageManager : ObservableObject { | ||
| var currentURL: URL? | ||
| var transaction = Transaction() | ||
| // Thread-safe callback properties | ||
| private let callbackQueue = DispatchQueue(label: "ImageManager.callbacks", qos: .userInitiated) | ||
Collaborator
| ||
| private var _successBlock: ((PlatformImage, Data?, SDImageCacheType) -> Void)? | ||
| private var _failureBlock: ((Error) -> Void)? | ||
| private var _progressBlock: ((Int, Int) -> Void)? | ||
| var successBlock: ((PlatformImage, Data?, SDImageCacheType) -> Void)? { | ||
| get { callbackQueue.sync { _successBlock } } | ||
| set { callbackQueue.sync { _successBlock = newValue } } | ||
| } | ||
| var failureBlock: ((Error) -> Void)? { | ||
| get { callbackQueue.sync { _failureBlock } } | ||
| set { callbackQueue.sync { _failureBlock = newValue } } | ||
| } | ||
| var progressBlock: ((Int, Int) -> Void)? { | ||
| get { callbackQueue.sync { _progressBlock } } | ||
| set { callbackQueue.sync { _progressBlock = newValue } } | ||
| } | ||
| public init() {} | ||
| @@ -96,9 +114,11 @@ public final class ImageManager : ObservableObject { | ||
| progress = 0 | ||
| } | ||
| self.indicatorStatus.progress = progress | ||
| // Capture progress callback in thread-safe way | ||
| let progressCallback = self.progressBlock | ||
| if let progressCallback = progressCallback { | ||
| DispatchQueue.main.async { | ||
| progressCallback(receivedSize, expectedSize) | ||
| } | ||
| } | ||
| }) { [weak self] (image, data, error, cacheType, finished, _) in | ||
| @@ -112,6 +132,10 @@ public final class ImageManager : ObservableObject { | ||
| // So previous View struct call `onDisappear` and cancel the currentOperation | ||
| return | ||
| } | ||
| // Capture completion callbacks in thread-safe way | ||
| let successCallback = self.successBlock | ||
| let failureCallback = self.failureBlock | ||
| withTransaction(self.transaction) { | ||
| self.image = image | ||
| self.error = error | ||
| @@ -122,9 +146,9 @@ public final class ImageManager : ObservableObject { | ||
| self.indicatorStatus.isLoading = false | ||
| self.indicatorStatus.progress = 1 | ||
| if let image = image { | ||
| successCallback?(image, data, cacheType) | ||
| } else { | ||
| failureCallback?(error ?? NSError()) | ||
| } | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading.Please reload this page.