Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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

Open
biovolt wants to merge1 commit intoSDWebImage:master
base:master
Choose a base branch
Loading
frombiovolt:master
Open
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 31 additions & 7 deletionsSDWebImageSwiftUI/Classes/ImageManager.swift
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -61,9 +61,27 @@ public final class ImageManager : ObservableObject {

var currentURL: URL?
var transaction = Transaction()
var successBlock: ((PlatformImage, Data?, SDImageCacheType) -> Void)?
var failureBlock: ((Error) -> Void)?
var progressBlock: ((Int, Int) -> Void)?

// Thread-safe callback properties
private let callbackQueue = DispatchQueue(label: "ImageManager.callbacks", qos: .userInitiated)
Copy link
Collaborator

@dreampiggydreampiggySep 3, 2025
edited
Loading

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

TheImageManager is not a shared instance. I remember it's just behaved as ObservedObject used for View (it like a ViewModel), so it's created everytime new URL is provided.

Does this (means, you create each DispatchQueue in each Manager instance) cause queue explosion ?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Valid point!
How do you propose I fix it?

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() {}

Expand DownExpand Up@@ -96,9 +114,11 @@ public final class ImageManager : ObservableObject {
progress = 0
}
self.indicatorStatus.progress = progress
if let progressBlock = self.progressBlock {
// Capture progress callback in thread-safe way
let progressCallback = self.progressBlock
if let progressCallback = progressCallback {
DispatchQueue.main.async {
progressBlock(receivedSize, expectedSize)
progressCallback(receivedSize, expectedSize)
}
}
}) { [weak self] (image, data, error, cacheType, finished, _) in
Expand All@@ -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
Expand All@@ -122,9 +146,9 @@ public final class ImageManager : ObservableObject {
self.indicatorStatus.isLoading = false
self.indicatorStatus.progress = 1
if let image = image {
self.successBlock?(image, data, cacheType)
successCallback?(image, data, cacheType)
} else {
self.failureBlock?(error ?? NSError())
failureCallback?(error ?? NSError())
}
}
}
Expand Down
Loading

[8]ページ先頭

©2009-2025 Movatter.jp