Skip to content

refactor(mobile): introduce image request registry on iOS #27486

Open
LeLunZ wants to merge 2 commits intobugfix-27365from
refactor/ios-image-registry
Open

refactor(mobile): introduce image request registry on iOS #27486
LeLunZ wants to merge 2 commits intobugfix-27365from
refactor/ios-image-registry

Conversation

@LeLunZ
Copy link
Copy Markdown
Collaborator

@LeLunZ LeLunZ commented Apr 3, 2026

Description

Base branch of this Pr is currently #27471

Currently LocalImageApiImpl and RemoteImageApiImpl each maintain their own mechanism for tracking inflight requests

  • Local used three DispatchQueues (assetQueue, requestQueue, cancelQueue) plus a raw [Int64: LocalImageRequest] dictionary, with manual add/remove/cancel static methods.
  • Remote used a raw os_unfair_lock held as a static var, with manual os_unfair_lock_lock/unlock calls around every dictionary access.

For me that just feels very inconsistent for the same "work".

Thats why I introduced Cancellable protocol and a generic RequestRegistry<T: Cancellable> that centralise all the request tracking together. Everything is done behind a UnfairLock which I think is the faster approach between locks, and queues.

In the RemoteImageApiImpl we can now remove the lock, and all manual lock/unlock calls as they are replaced by the registry.

In the LocalImageApiImpl we can remove the cancelQueue and requestQueue as both are handled by the lock in the registry.

Additional change:

removed assetQueue as NSCache is already thread safe.

How Has This Been Tested?

  • on an emulator

Checklist:

  • I have carefully read CONTRIBUTING.md
  • I have performed a self-review of my own code
  • I have made corresponding changes to the documentation if applicable
  • I have no unrelated changes in the PR.
  • I have confirmed that any new dependencies are strictly necessary.
  • I have written tests for new code (if applicable)
  • I have followed naming conventions/patterns in the surrounding code
  • All code in src/services/ uses repositories implementations for database calls, filesystem operations, etc.
  • All code in src/repositories/ is pretty basic/simple and does not have any immich specific logic (that belongs in src/services/)

Please describe to which degree, if any, an LLM was used in creating this pull request.

unfair lock implementation

@LeLunZ LeLunZ requested a review from mertalev April 3, 2026 13:16
@LeLunZ LeLunZ requested a review from shenlong-tanwen as a code owner April 3, 2026 13:16
@immich-push-o-matic
Copy link
Copy Markdown

Label error. Requires exactly 1 of: changelog:.*. Found: 📱mobile. A maintainer will add the required label.

1 similar comment
@immich-push-o-matic
Copy link
Copy Markdown

Label error. Requires exactly 1 of: changelog:.*. Found: 📱mobile. A maintainer will add the required label.

@LeLunZ LeLunZ changed the title refactor(mobile): introduce request registry on iOS refactor(mobile): introduce image request registry on iOS Apr 3, 2026
Comment on lines +20 to +22
func cancel(requestId: Int64) {
remove(requestId: requestId)?.cancel()
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Is there much point to this and the Cancellable protocol? It seems like the caller could easily just do this.

Copy link
Copy Markdown
Collaborator Author

@LeLunZ LeLunZ Apr 4, 2026

Choose a reason for hiding this comment

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

Yes, Cancellable exists to make registry.cancel() possible.
In #27489 it gets replaced with the ImageRequest base class which also has the cancel function, but because we use a base class we don't really need a protocol anymore.


But you are right, we only have 2 callers, so this helper isn't really that necessary.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think it's more composable to have the caller interact with the request and keep the registry lean.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants