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(android): remove the redundant PageSelectedEvent dispatch causing an ANR on RN 0.72 #770

Merged
merged 1 commit into from
Sep 25, 2023

Conversation

igorbej
Copy link
Contributor

@igorbej igorbej commented Sep 12, 2023

This PR removes a redundant PageSelectedEvent dispatch causing an ANR on RN 0.72.

Closes #752

Summary

In the following function:

fun goTo(root: NestedScrollableHost?, selectedPage: Int, scrollWithAnimation: Boolean) {
if (root == null) {
return
}
val view = PagerViewViewManagerImpl.getViewPager(root)
Assertions.assertNotNull(view)
val childCount = view.adapter?.itemCount
val canScroll = childCount != null && childCount > 0 && selectedPage >= 0 && selectedPage < childCount
if (canScroll) {
PagerViewViewManagerImpl.setCurrentItem(view, selectedPage, scrollWithAnimation)
val reactContext = view.context as ReactContext
UIManagerHelper.getEventDispatcherForReactTag(reactContext, view.id)?.dispatchEvent(
PageSelectedEvent(view.id, selectedPage)
)
}
}

On the new architecture, using RN 0.72, the event in L178-L180 is dispatched with an incorrect view.id of -1 which apparently trips React Native up causing a silent error leading to an ANR.

One way to fix it is to adjust the ID used with this dispatch, e.g. by replacing view.id with root.id, which contains the correct ID value, which solves the issue.

However, the PagerViewViewManagerImpl.setCurrentItem(...) call in L176 also (implicitly) triggers a PageSelectedEvent dispatch, via the onPageSelected callback:

override fun onPageSelected(position: Int) {
super.onPageSelected(position)
UIManagerHelper.getEventDispatcherForReactTag(reactContext, host.id)?.dispatchEvent(
PageSelectedEvent(host.id, position)
)
}

Which makes the first dispatch redundant. Hence, this PR removes this dispatch on both architectures (consulted with @troZee).

Test Plan

Test the example app on both architectures and confirm there's no ANR.

Compatibility

OS Implemented
iOS
Android

Checklist

  • I have tested this on a device and a simulator
  • I added the documentation in README.md
  • I updated the typed files (TS and Flow)

@troZee troZee merged commit 51738d8 into master Sep 25, 2023
3 checks passed
@troZee troZee deleted the fix/igorbej/android-setPage-ANR-issue branch September 25, 2023 11:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Android] Got ANR Error in setPage and setPageWithoutAnimation
2 participants