Skip to content
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

@capacitor/camera does not honor users selection order #1950

Open
l0ll098-at-mmfg opened this issue Dec 5, 2023 · 2 comments · May be fixed by #1951
Open

@capacitor/camera does not honor users selection order #1950

l0ll098-at-mmfg opened this issue Dec 5, 2023 · 2 comments · May be fixed by #1951

Comments

@l0ll098-at-mmfg
Copy link

@capacitor/camera does not honor users selection order

Plugin(s)

@capacitor/camera

Capacitor Version

Latest Dependencies:

  @capacitor/cli: 5.5.1
  @capacitor/core: 5.5.1
  @capacitor/android: 5.5.1
  @capacitor/ios: 5.5.1

Installed Dependencies:

  @capacitor/cli: 5.2.2
  @capacitor/core: 5.2.2
  @capacitor/android: 5.2.2
  @capacitor/ios: 5.2.2

Platform(s)

iOS

Current Behavior

Method Camera.pickImages exposed by the @capacitor/camera plugin does not return image details using the same order in which users selected those images. Furthermore, selecting images over and over again, may yield different results each time (result of the pickImages is not stable from a sorting point of view).

In other words, if I have images 1 through 50, Camera.pickImages may return images in any arbitrary order. So, rolling with the same example as before, if I select images 1 through 50, I may get details about them in orders like:

  • 1,2,3,4,...49,50 (if I'm very lucky)
  • 2,1,3,4,7,10,5,....50,49
  • or in any other permutation of those 50 numbers.

This is different from the behavior observed on Android where details about each image are returned using the same ordering in which user picked them. So, for example, in the first position of the array returned by the pickImages method, I'll find details about the firstly selected image. In the second position of that array, I'll find details of the second image selected by the user, and so on for each other image.

Same thing goes for the underlying <input type="file" multiple="multiple" /> we find in the web implementation of the @capacitor/camera plugin.

Expected Behavior

Expected result is that the order of selection is preserved, even when using the iOS implementation.
Additionally, selecting the same set of images multiple times, should yield to the same result.

Code Reproduction

const { photos } = await Camera.pickImages({
  quality: 100,
});

console.log(photos);

If the above snippet uses the web or Android implementations of the pickImages method, photos will be an array where each item is presented in the same order in which user selected images. If the above snippet uses the iOS implementation, photos may have different orders. This is especially true and evident when selecting many images (with 30 images you are pretty much certain to encounter this problem).

Additional Context

I did some debugging and it seems like the problem lies in the function picker defined in the CameraPlugin extension (file CameraPlugin.swift).
To be more precise, this problem boils down to a race condition in the handling of the result from self?.processedImage, where each processedImage is appended to an array in a closure of the asynchronous method loadObject.

We've already developed a patch for this problem and we can confirm that this solved the problem our users encountered.

@ionitron-bot ionitron-bot bot added the triage label Dec 5, 2023
@ionitron-bot ionitron-bot bot removed the triage label Dec 5, 2023
l0ll098-at-mmfg added a commit to l0ll098-at-mmfg/capacitor-plugins that referenced this issue Dec 5, 2023
Due to a race condition, pickImages on iOS did not honor the order in which users selected images.
Fixes: ionic-team#1950
@Ionitron
Copy link
Collaborator

This issue needs more information before it can be addressed.
In particular, the reporter needs to provide a minimal sample app that demonstrates the issue.
If no sample app is provided within 15 days, the issue will be closed.

Please see the Contributing Guide for how to create a Sample App.

Thanks!
Ionitron 💙

@l0ll098-at-mmfg
Copy link
Author

l0ll098-at-mmfg commented Mar 1, 2024

Here's a very simple app to demonstrate the issue

It shows a brief explanation on how to reproduce the issue and a button that, once pressed, allows you to select a bunch of images. Select some, let's say six or more, and keep in mind the order in which you selected them. Then tap the "done" button so that they are returned to the JS part of the application. After a couple of seconds you will see those images you selected rendered inside the Capacitor app. Take a look at them and you should see that they are not display in the same order you have picked them, even though there is no JS to explicitly shuffle them.

Note: since this is caused by a race condition in the native iOS part of the Camera plugin, it may require a couple of attempts before being able to reproduce the error

If you take a look at the web app in your browser, you will see that the ordering of the picked images is preserved, no matter what.

Here you can also find a video I recorded using the iOS Simulator that shows this application in action.
As you can see, I'm picking images in the order they are presented by the iOS photo picking dialog. So, once those images are rendered in the webview, I'm expecting to see them presented in the same order.
In other word, the image with those violet and yellow flowers should be displayed as my first image. The photo depicting that foggy waterfall should be the second one. Rolling with the same example, the image showing some kind of plant/tree leaves should be shown as my fifth image.
However, once picked images are rendered inside the webview, you can see that we have the violet and yellow flowers (this is correct), an another image of some grass and flowers (we were expecting this to be our 6th and last image), the photo of leaves (this should have been the 5th one), the foggy waterfall (should have been the second one), and so on.

Simulator.Screen.Recording.-.iPhone.15.Pro.-.2024-03-01.at.10.52.38.mov

Changes in associated PR (#1951) however, fix this issue by ensuring a consistent ordering

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

Successfully merging a pull request may close this issue.

3 participants