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

Fix PickerAndroid will reset selected value during items update. #24793

Closed
wants to merge 1 commit into from

Conversation

Kudo
Copy link
Contributor

@Kudo Kudo commented May 10, 2019

Summary

Fixes #13351

Two root causes:

  1. Android Spinner will reset selection to undefined after setAdapter()
    which will trigger onValueChange().
    The behavior is not expected for RN.
    And the solution is to setSelection() explicitly

  2. In original implementation, it setups items immediately,
    but delays the selected setup after update transaction.
    There will be some race condition and incosistency
    if update items only.
    The fix will do the setup all after update transaction.

Changelog

[Android] [Fixed] - Fix #13351 PickerAndroid will reset selected value during items update.

Test Plan

  1. Test the test case as @stochris reported in https://snack.expo.io/Sy1JClEag.
  2. Test RNTester Picker sample

Summary:
  Two root causes:
  1. Android Spinner will reset selection to undefined after setAdapter()
     which will trigger onValueChange().
     The behavior is not expected for RN.
     And the solution is to setSelection() explicitly

  2. In original implementation, it setups `items` immediately,
     but delays the `selected` after update transaction.
     There will be some race condition and incosistency
     if update `items` only.
     The fix will update both after transaction.

  Fixes facebook#13351
@facebook-github-bot facebook-github-bot added CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Contributor A React Native contributor. labels May 10, 2019
Copy link
Contributor

@cpojer cpojer left a comment

Choose a reason for hiding this comment

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

Awesome, thank you so much for making a fix! :)

Copy link
Contributor

@facebook-github-bot facebook-github-bot left a comment

Choose a reason for hiding this comment

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

@cpojer is landing this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

@react-native-bot
Copy link
Collaborator

This pull request was successfully merged by @Kudo in 310cc38.

When will my fix make it into a release? | Upcoming Releases

@axe-fb
Copy link
Contributor

axe-fb commented Jun 14, 2019

@Kudo - Looks like this PR is causing an increase in ArrayOutOfBoundsException at getView. Do you have context on why this is happening ?

I dont have context on this - do you think this is related ?

@Kudo
Copy link
Contributor Author

Kudo commented Jun 17, 2019

@axe-fb I've sent a PR #25276, hopefully that will fix your problem. Thanks for the reporting.

facebook-github-bot pushed a commit that referenced this pull request Jun 20, 2019
…e from a long list (#25276)

Summary:
axe-fb reported this side effect from my previous commit in #24793 (comment)
After revisited the implementation of Android Spinner, it seems the formal way to update existing adapter is mutating it, i.e. `arrayAdapter.clear()` & `arrayAdapter.addAll()` to update a Spinner Adapter.
`setAdapter()` will reset everything including `mDataChanged`.
A race condition may happens between rendering a long picker list and reseting adapter.
Here is a code snippet: https://snack.expo.io/kudochien/80f810
To reproduce the issue, please select large item (e.g. 500) first and click the button right hand side.
Please not to verify this on Expo directly in the meantime, because Expo with RN 0.59 does not include my previous commit.

## Changelog

[Android] [Fixed] - Fix Picker ArrayOutOfBoundsException during Picker.Item update from a long list
Pull Request resolved: #25276

Test Plan:
1. Check the test case https://snack.expo.io/kudochien/80f810 will have exception or not.
2. Regression of https://snack.expo.io/Sy1JClEag from #13351
3. Regression of https://snack.expo.io/kudochien/android-picker-issue from #22821
4. RNTester Picker example

Reviewed By: mdvacca

Differential Revision: D15857426

Pulled By: axe-fb

fbshipit-source-id: 8ef902447fdd1b8aeab50ad061545cd14c735e51
M-i-k-e-l pushed a commit to M-i-k-e-l/react-native that referenced this pull request Mar 10, 2020
…e from a long list (facebook#25276)

Summary:
axe-fb reported this side effect from my previous commit in facebook#24793 (comment)
After revisited the implementation of Android Spinner, it seems the formal way to update existing adapter is mutating it, i.e. `arrayAdapter.clear()` & `arrayAdapter.addAll()` to update a Spinner Adapter.
`setAdapter()` will reset everything including `mDataChanged`.
A race condition may happens between rendering a long picker list and reseting adapter.
Here is a code snippet: https://snack.expo.io/kudochien/80f810
To reproduce the issue, please select large item (e.g. 500) first and click the button right hand side.
Please not to verify this on Expo directly in the meantime, because Expo with RN 0.59 does not include my previous commit.

## Changelog

[Android] [Fixed] - Fix Picker ArrayOutOfBoundsException during Picker.Item update from a long list
Pull Request resolved: facebook#25276

Test Plan:
1. Check the test case https://snack.expo.io/kudochien/80f810 will have exception or not.
2. Regression of https://snack.expo.io/Sy1JClEag from facebook#13351
3. Regression of https://snack.expo.io/kudochien/android-picker-issue from facebook#22821
4. RNTester Picker example

Reviewed By: mdvacca

Differential Revision: D15857426

Pulled By: axe-fb

fbshipit-source-id: 8ef902447fdd1b8aeab50ad061545cd14c735e51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Contributor A React Native contributor. Merged This PR has been merged. Platform: Android Android applications.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Picker Component resets when items list changes
5 participants