-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
refactor(mobile): iOS introduce ImageRequest base class with unified cancellation and finish helpers #27489
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
refactor(mobile): iOS introduce ImageRequest base class with unified cancellation and finish helpers #27489
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
de9c217
refactor: replace DispatchQueue + DispatchSemaphore with OperationQue…
LeLunZ d71afee
implement RequestRegistry and UnfairLock for managing cancellable req…
LeLunZ 272d0ce
implement requests registry for local and remote image processing
LeLunZ f5f3368
remove Cancellable protocol and cancel method from request registry
LeLunZ 2149914
refactor: introduce ImageRequest base class with unified cancellation…
LeLunZ 656c7c2
refactor: add get method to RequestRegistry and streamline request re…
LeLunZ 44d0653
add guard to cancel to prevent double onCancel calls
LeLunZ 7a934ce
fix duplicate code merge issue
LeLunZ d9fb3eb
Merge remote-tracking branch 'upstream/main' into refactor/ios-image-…
LeLunZ 421bbb9
refactor(ios): enhance finish method to return callback status
LeLunZ d5e23c0
remove unfitting methods form ImageRequest.swift and fix memory issue
LeLunZ b53243c
revert bad merge
LeLunZ File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,58 @@ | ||
| import Foundation | ||
|
|
||
| class ImageRequest { | ||
| private struct State { | ||
| var isCancelled = false | ||
| var callback: ((Result<[String: Int64]?, any Error>) -> Void)? | ||
| } | ||
|
|
||
| private let state: Mutex<State> | ||
|
|
||
| var isCancelled: Bool { | ||
| state.withLock { $0.isCancelled } | ||
| } | ||
|
|
||
| init(callback: @escaping (Result<[String: Int64]?, any Error>) -> Void) { | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| self.state = Mutex(State(callback: callback)) | ||
| } | ||
|
|
||
| func cancel() { | ||
| let cb = state.withLock { state in | ||
| state.isCancelled = true | ||
| defer { state.callback = nil } | ||
| return state.callback | ||
| } | ||
| guard let cb else { return } | ||
| onCancel() | ||
| cb(ImageProcessing.cancelledResult) | ||
| } | ||
|
|
||
| /// Delivers the result to the callback. Returns true if the callback was called, false if it was already consumed. | ||
| @discardableResult | ||
| func finish(with result: Result<[String: Int64]?, any Error>) -> Bool { | ||
| let cb = state.withLock { state in | ||
| defer { state.callback = nil } | ||
| return state.callback | ||
| } | ||
| guard let cb else { return false } | ||
| cb(result) | ||
| return true | ||
| } | ||
|
|
||
| func onCancel() {} | ||
| } | ||
|
|
||
| struct RequestRegistry<T: AnyObject & Sendable>: ~Copyable, Sendable { | ||
| private let requests = Mutex<[Int64: T]>([:]) | ||
|
|
||
| func add(requestId: Int64, request: T) { | ||
| requests.withLock { $0[requestId] = request } | ||
| } | ||
|
|
||
| func get(requestId: Int64) -> T? { | ||
| requests.withLock { $0[requestId] } | ||
| } | ||
|
|
||
| @discardableResult | ||
| func remove(requestId: Int64) -> T? { | ||
| requests.withLock { $0.removeValue(forKey: requestId) } | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| import Foundation | ||
|
|
||
| // Can be replaced with OSAllocatedUnfairLock when the deployment target is iOS 16+ | ||
| final class UnfairLock { | ||
| private let _lock: UnsafeMutablePointer<os_unfair_lock> | ||
|
|
||
| init() { | ||
| _lock = .allocate(capacity: 1) | ||
| _lock.initialize(to: os_unfair_lock()) | ||
| } | ||
|
|
||
| deinit { | ||
| _lock.deinitialize(count: 1) | ||
| _lock.deallocate() | ||
| } | ||
|
|
||
| @discardableResult | ||
| func withLock<T>(_ body: () throws -> T) rethrows -> T { | ||
| os_unfair_lock_lock(_lock) | ||
| defer { os_unfair_lock_unlock(_lock) } | ||
| return try body() | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mertalev back to your review questions in the other PR:
yes this does need a lock.
The swift docs say that the swift memory model has a conflict if all three conditions are met:
_isCancelled is written in cancel() (potentially from the Dart/main thread) and read in isCancelled (from the OperationQueue worker thread).
That's a concurrent write + read on the same memory address.
Swift also clarifies that plain variable access is not atomic:
So, a plain Bool write/read is nonatomic, without the lock we have two nonatomic, overlapping, one read, a written...means undefined behaviour.
This was actually a "bug" that was in the pre existing code.