packages icon indicating copy to clipboard operation
packages copied to clipboard

[image_picker] Move I/O operations to a separate thread

Open JeroenWeener opened this issue 2 years ago • 0 comments

Many I/O operations in the image_picker are currently carried out on the main thread (at least on Android). This blocks the UI (main) thread, freezing the UI and sometimes even causing an ANR dialog to pop up.

With these PR, many of said I/O operations now run on a separate thread. Specifically, it executes all code that is run in response to a picking result on a separate thread. So when an image is picked, for example, the callback will be ran outside of the UI thread. This change was rather easy, as said code was already making use of async paradigms.

The I/O operations carried out for caching logic (ImagePickerDelegate.retrieveLostImage()) are not changed by this PR, as my guess is that this would call for breaking changes to the API, as the result is currently returned synchronously instead.

When running the example app in StrictMode and picking an image, the problem becomes apparent, as it reports disk read/write violations. This was described in #100966. I have verified that StrictMode no longer reports disk read/write violations after the changes. To check the output of StrictMode, I ran the example app with the changes listed here.

This PR closes several issues related to ANRs.

Also, it partly implements what is requested in #91393.

Pre-launch Checklist

  • [x] I read the Contributor Guide and followed the process outlined there for submitting PRs.
  • [x] I read the Tree Hygiene wiki page, which explains my responsibilities.
  • [x] I read and followed the relevant style guides and ran the auto-formatter. (Unlike the flutter/flutter repo, the flutter/packages repo does use dart format.)
  • [x] I signed the CLA.
  • [x] The title of the PR starts with the name of the package surrounded by square brackets, e.g. [shared_preferences]
  • [x] I listed at least one issue that this PR fixes in the description above.
  • [x] I updated pubspec.yaml with an appropriate new version according to the pub versioning philosophy, or this PR is exempt from version changes.
  • [x] I updated CHANGELOG.md to add a description of the change, following repository CHANGELOG style.
  • [x] I updated/added relevant documentation (doc comments with ///).
  • [x] I added new tests to check the change I am making, or this PR is test-exempt.
  • [x] All existing and new tests are passing.

JeroenWeener avatar Mar 21 '23 15:03 JeroenWeener