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

App Crash - UIViewControllerHierarchyInconsistency #167

Closed
ozgursoy opened this issue May 5, 2020 · 58 comments · Fixed by #432
Closed

App Crash - UIViewControllerHierarchyInconsistency #167

ozgursoy opened this issue May 5, 2020 · 58 comments · Fixed by #432
Labels

Comments

@ozgursoy
Copy link
Contributor

ozgursoy commented May 5, 2020

Bug

When i open the app, it crashes sometimes ( %20 crash rate )
There is no log on console, Flipper can't catch any error, Only Crashlytics catches the error.

  • Android is working very well.
  • There is no problem on version 3.3.0

Screen Shot 2020-05-05 at 17 21 56

react-native info output:

System:
    OS: macOS 10.15.3
    CPU: (4) x64 Intel(R) Core(TM) i5-5257U CPU @ 2.70GHz
    Memory: 162.63 MB / 8.00 GB
    Shell: 5.7.1 - /bin/zsh
  Binaries:
    Node: 12.11.1 - /usr/local/bin/node
    Yarn: 1.17.3 - /usr/local/bin/yarn
    npm: 6.14.5 - /usr/local/bin/npm
    Watchman: 4.9.0 - /usr/local/bin/watchman
  SDKs:
    iOS SDK:
      Platforms: iOS 13.2, DriverKit 19.0, macOS 10.15, tvOS 13.2, watchOS 6.1
    Android SDK: Not Found
  IDEs:
    Android Studio: Not Found
    Xcode: 11.3.1/11C505 - /usr/bin/xcodebuild
  Languages:
    Python: 2.7.16 - /usr/bin/python
  npmPackages:
    @react-native-community/cli: Not Found
    react: 16.13.1 => 16.13.1 
    react-native: 0.62.2 => 0.62.2 
  npmGlobalPackages:
    *react-native*: Not Found

Library version: 4.0.0

@troZee troZee added the Resolution: Needs Repro This issue could be improved with a demo to reproduce the issue. label May 5, 2020
@troZee
Copy link
Member

troZee commented May 5, 2020

It is strange, that iOS fixes does not work for you.

You can checkout changelog here:
https://github.com/react-native-community/react-native-viewpager/releases/tag/v4.0.0

Could you provide reproducible example code ?

@krystiansliwa have you got any ideas ?

@troZee troZee added the ios label May 5, 2020
@troZee troZee changed the title IOS App Crash - UIViewControllerHierarchyInconsistency App Crash - UIViewControllerHierarchyInconsistency May 5, 2020
@ozgursoy
Copy link
Contributor Author

ozgursoy commented May 5, 2020

Extra info:
We use redux-offline.
When i disable redux-online, works perfectly.
This is really strange because there is no problem on version 3.3.0

@srfaytkn
Copy link

srfaytkn commented May 5, 2020

+1

@ahk093
Copy link

ahk093 commented May 5, 2020

I have the same issue.

@troZee
Copy link
Member

troZee commented May 5, 2020

@srfaytkn @ahk093 Could you provide any reproducible example ?

@ozgursoy
Copy link
Contributor Author

ozgursoy commented May 6, 2020

@troZee
I replaced childViewController.view = view;
to
[childViewController.view addSubview: view];
then issue is solved

I can't provide any reproducible example right now, i have to create a new project for this

@troZee
Copy link
Member

troZee commented May 6, 2020

@Farad4y
I checked 3 fixes, which has been included into 4.0 and I can't find any cause of above errors.
It is hard to say without reproducible example, where the problem is.

Now as workaround. You can fix it locally using patch-package.

I can't provide any reproducible example right now, i have to create a new project for this

No worries :D I am waiting for reproducible example

@genglei01
Copy link

@troZee
I have the same issue and try @Farad4y's solution, the error seems to be fixed/(I try it many times)

I replaced childViewController.view = view;
to
[childViewController.view addSubview: view];

@Inexpressible
Copy link

This error happens for us with screens that have multiple carousels. The suggested patch fixed the issue.

@ozgursoy
Copy link
Contributor Author

@genglei01 @Inexpressible Could you provide example project? My project that crashes is very big, so i can't simplified it

@tekmin
Copy link

tekmin commented Jun 1, 2020

We have the same issue, but not reproducible from our project too. I will try on @Farad4y solutions first.

@jpamarohorta
Copy link

I had the same issue and @Farad4y patch fixed it. Here is the patch created using patch-package:

diff --git a/node_modules/@react-native-community/viewpager/ios/ReactNativePageView.m b/node_modules/@react-native-community/viewpager/ios/ReactNativePageView.m
index cbb3b36..d7e8893 100644
--- a/node_modules/@react-native-community/viewpager/ios/ReactNativePageView.m
+++ b/node_modules/@react-native-community/viewpager/ios/ReactNativePageView.m
@@ -379,7 +379,7 @@ - (void)setReactViewControllers:(NSInteger)index
 
 - (UIViewController *)createChildViewController:(UIView *)view {
     UIViewController *childViewController = [[UIViewController alloc] init];
-    childViewController.view = view;
+    [childViewController.view addSubview: view];
     return childViewController;
 }

@genglei01
Copy link

Is there any release schedule for this bug?

@troZee
Copy link
Member

troZee commented Jun 10, 2020

Is there any release schedule for this bug?

I will merge and release it, once someone provides me an example to test it.

@jpamarohorta
Copy link

In my case the crash occurred 10% of the times this component was mounted (stats from Firebase), so it's difficult to track and make an example that always crashes.

@stami
Copy link

stami commented Jun 30, 2020

There may be a race condition causing this crash.

UIViewControllerHierarchyInconsistency
A view can only be associated with at most one view controller at a time!

In ReactNativePageView.m the createChildViewController is called with the same array of UIViews (self.reactSubviews) from renderChildrenViewControllers and addPages.

The @Farad4y 's patch fixes the issue because addSubview removes the previous superview if needed before adding it to the new superview.

@RodrigoSosa95
Copy link

+1 on this issue, even on version 4.1.0 happens to me when called viewPager.current.setPage.

@troZee
Copy link
Member

troZee commented Jul 16, 2020

It has been resolved here #189 in 4.1.0 version.

@troZee troZee closed this as completed Jul 16, 2020
@eladgel
Copy link

eladgel commented Jul 18, 2020

Has this been really fixed? Making sure before I upgrade my library. @RodrigoSosa95 ?

@chj-damon
Copy link

I'm facing the same problem. I'll try to upgrade the version and see if it's fixed.

@enigmablue
Copy link

enigmablue commented Aug 1, 2020

Also faced the same problem. omg i hope this fixes.

Have upgraded to "@react-native-community/viewpager": "^4.1.3", and will monitor

@troZee
Copy link
Member

troZee commented Aug 3, 2020

I reopening this issue, because bug still exist

@troZee troZee reopened this Aug 3, 2020
@troZee troZee removed the Resolution: Needs Repro This issue could be improved with a demo to reproduce the issue. label Aug 3, 2020
@enigmablue
Copy link

enigmablue commented Aug 3, 2020

Yes i'm getting the same error. But this time, it's not (randomly) killing the app completely, just consistently throwing an error. My current unglamorous workaround to fix the race condition is to set a timeout on the setPage as i think its having a race condition on the list in the state that is referred in the viewpager:

const listener = (shares ,changes) => {
...
 changes.insertions.forEach((index) => {
let updatedShares = getUpdatedShares()

     setTimeout(()=>{
       setDisplayList(updatedShares);
       }, 100) //needed to set timeout here because of some other issue on pausing video

       setTimeout(()=>{
         viewPager.current && viewPager.current.setPage(0)
       }, 200) //viewpager race condition problem with setpage
}
...
}

My viewpager code

<ViewPager
        style={{flex: 1}}
        initialPage={0}
        ref={viewPager}
        orientation="vertical"
        onPageScroll={onPageScroll}
        onPageSelected={onPageSelected}
        onPageScrollStateChanged={onPageScrollStateChanged}
        >
        {displayList && displayList.map((share,index) =>
          renderSlide(share, index)
        )}
        </ViewPager>

@joebochill
Copy link

I narrowed down the problem in my case to version 4.1.0.

4.0.1: works as expected
4.1.0: throws the ViewController error above and the app crashes.

@bonlemuel
Copy link

@joebochill

I confirm that 4.0.1 is also working on my end.

Thank you! Saved me a lot of time figuring out a fix.

@cjhines
Copy link

cjhines commented Jul 30, 2021

This seems to have to reappeared (5.4.0 + iOS).

Screenshot 2021-07-30 at 09 00 48

Exception thrown while executing UI block: A view can only be associated with at most one view controller at a time! View <RCTView: 0x7fe85544df90; reactTag: 7413; frame = (0 0; 0 0); layer = <CALayer: 0x6000002f6180>> is associated with <UIViewController: 0x7fe8550becb0>. Clear this association before associating this view with <UIViewController: 0x7fe854f78b50>.

__44-[RCTUIManager flushUIBlocksWithCompletion:]_block_invoke
    RCTUIManager.m:1193
__44-[RCTUIManager flushUIBlocksWithCompletion:]_block_invoke.493
__RCTExecuteOnMainQueue_block_invoke
_dispatch_call_block_and_release
_dispatch_client_callout
_dispatch_main_queue_callback_4CF
__CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__
__CFRunLoopRun
CFRunLoopRunSpecific
GSEventRunModal
-[UIApplication _run]
UIApplicationMain
main
start
0x0

@sezginriggs
Copy link

In addition to above reply, I also have the same error with IOS 13.7

@troZee
Copy link
Member

troZee commented Aug 9, 2021

I am getting the same error as @bonlemuel. Mine does not seem to be related to dialogs though. I get the error when the ViewPager is rendered conditionally after some asynchronous data is loaded.

Simplified example:

    return conditionSatisfied ? (
        <View>
            <SafeAreaView>
                <ViewPager
                    ref={viewPager}
                    initialPage={0}
                >
                    <View style={{height: 500, width: 300, backgroundColor: 'orange'}}/>
                    <View style={{height: 500, width: 300, backgroundColor: 'red'}}/>
                    <View style={{height: 500, width: 300, backgroundColor: 'blue'}}/>
                    <View style={{height: 500, width: 300, backgroundColor: 'green'}}/>
                </ViewPager>
            </SafeAreaView>
        </View>
    ) : (
        <LoadingComponent />
    ) 

The other component doesn't seem to matter...I can return null and still get the same error.

Downgrading to 3.x makes the issue go away, but I was using 4.x before I upgraded my RN version to 0.63.

I was trying to reproduce using below code:

import React, { useMemo } from 'react';
import { StyleSheet, View, SafeAreaView, Animated, Text } from 'react-native';

import PagerView from 'react-native-pager-view';

import { LikeCount } from './component/LikeCount';
import { NavigationPanel } from './component/NavigationPanel';
import { useNavigationPanel } from './hook/useNavigationPanel';

const AnimatedPagerView = Animated.createAnimatedComponent(PagerView);

export function BasicPagerViewExample() {
  const { ref, ...navigationPanel } = useNavigationPanel();

  const [isLoading, setLoading] = React.useState(true);

  const pages = useMemo(
    () =>
      navigationPanel.pages.map((page, index) => (
        <View key={page.key} style={page.style} collapsable={false}>
          <LikeCount />
          <Text testID={`pageNumber${index}`}>{`page number ${index}`}</Text>
        </View>
      )),
    [navigationPanel.pages]
  );

  React.useEffect(() => {
    const timer = setTimeout(() => {
      setLoading(false);
    }, 1000);
    return () => {
      clearTimeout(timer);
    };
  }, []);

  return isLoading ? (
    <View />
  ) : (
    <SafeAreaView style={styles.container}>
      <AnimatedPagerView
        //@ts-ignore
        testID="pager-view"
        ref={ref}
        style={styles.PagerView}
        initialPage={2}
        layoutDirection="ltr"
        overdrag={navigationPanel.overdragEnabled}
        scrollEnabled={navigationPanel.scrollEnabled}
        onPageScroll={navigationPanel.onPageScroll}
        onPageSelected={navigationPanel.onPageSelected}
        onPageScrollStateChanged={navigationPanel.onPageScrollStateChanged}
        pageMargin={10}
        // Lib does not support dynamically orientation change
        orientation="horizontal"
        // Lib does not support dynamically transitionStyle change
        transitionStyle="scroll"
        showPageIndicator={navigationPanel.dotsEnabled}
      >
        {pages}
      </AnimatedPagerView>
      <NavigationPanel {...navigationPanel} />
    </SafeAreaView>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: 'white',
  },
  image: {
    width: 300,
    height: 200,
    padding: 20,
  },
  PagerView: {
    flex: 1,
  },
});

and I was not able to do that

@troZee troZee reopened this Aug 9, 2021
@wildseansy
Copy link

wildseansy commented Aug 10, 2021

From my tests, this issue is in v5.3.4 too (released on the same day). v5.3.3 is the safest version in my investigation

This seems to have to reappeared (5.4.0 + iOS).

Screenshot 2021-07-30 at 09 00 48
Exception thrown while executing UI block: A view can only be associated with at most one view controller at a time! View <RCTView: 0x7fe85544df90; reactTag: 7413; frame = (0 0; 0 0); layer = <CALayer: 0x6000002f6180>> is associated with <UIViewController: 0x7fe8550becb0>. Clear this association before associating this view with <UIViewController: 0x7fe854f78b50>.

__44-[RCTUIManager flushUIBlocksWithCompletion:]_block_invoke
    RCTUIManager.m:1193
__44-[RCTUIManager flushUIBlocksWithCompletion:]_block_invoke.493
__RCTExecuteOnMainQueue_block_invoke
_dispatch_call_block_and_release
_dispatch_client_callout
_dispatch_main_queue_callback_4CF
__CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__
__CFRunLoopRun
CFRunLoopRunSpecific
GSEventRunModal
-[UIApplication _run]
UIApplicationMain
main
start
0x0

@theambidextrous
Copy link

this issue still persists in 5.4.0. Am using it as a dependency of @react-navigation/material-top-tabs

Here is the error:

Exception thrown while executing UI block: A view can only be associated with at most one view controller at a time! View <RCTView: 0x7fe9ef208a00; reactTag: 313; frame = (0 0; 0 0); clipsToBounds = YES; layer = <CALayer: 0x600001061760>> is associated with <UIViewController: 0x7fe9ed45ed30>. Clear this association before associating this view with <UIViewController: 0x7fe9ed656d70>.

__44-[RCTUIManager flushUIBlocksWithCompletion:]_block_invoke
RCTUIManager.m:1193
__44-[RCTUIManager flushUIBlocksWithCompletion:]_block_invoke.493
__RCTExecuteOnMainQueue_block_invoke
_dispatch_call_block_and_release
_dispatch_client_callout
_dispatch_main_queue_callback_4CF
CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE
__CFRunLoopRun
CFRunLoopRunSpecific
GSEventRunModal
-[UIApplication _run]
UIApplicationMain
main
start

@msimon
Copy link

msimon commented Aug 16, 2021

Downgrading to 4.2.4 worked for me on IOS.

Something else that I notice, is that this bug seems to happen when the app is cold. I never had it randomly, only when I opened the app for the first time after a while.

@theambidextrous
Copy link

Downgrading to 4.2.4 worked for me on IOS.

Something else that I notice, is that this bug seems to happen when the app is cold. I never had it randomly, only when I opened the app for the first time after a while.

Seems so. For me, the apps runs well on first start, but the error shows when i restart the RN Bundler or when i close and reopen the app on the simulator.

let me try downgrade to 4.2.4

@alimek
Copy link

alimek commented Aug 17, 2021

I found workaround for that issue, delay rendering ViewPager by 50ms, its enough to fix issue for me and almost not visible to end user.

@bonlemuel
Copy link

Downgrading works for me as well.

@theambidextrous
Copy link

Yes, i can confirm the same. either version 4.2.4 or 5.3.3 ... they both work, so i went with 5.3.3

@rborn
Copy link

rborn commented Aug 19, 2021

Worked for me with a combination between delaying the render with 50ms and downgrade to 5.3.0 😿

@Bardiamist
Copy link

Bardiamist commented Aug 23, 2021

I have this error now. Appeared after update React native to 0.65.0 (coincidence?). Problem related with some view from PagerView.

Screenshot Screenshot 2021-08-23 at 16 32 11

upd: looks revert from 5.4.0 to 5.3.3 helped

@troZee
Copy link
Member

troZee commented Aug 23, 2021

Can anyone test this code ? #432

Instructions:

@Bardiamist
Copy link

Can anyone test this code ? #432

Instructions:

I tested. Looks no crash (it not 100% reproducable for me so tested 6 times without errors). But I can't change page by gesture with this code.

@troZee
Copy link
Member

troZee commented Aug 23, 2021

Can anyone test this code ? #432
Instructions:

I tested. Looks no crash (it not 100% reproducible for me so tested 6 times without errors). But I can't change page by gesture with this code.

Thank you for the replay. I am on it.

@troZee
Copy link
Member

troZee commented Aug 23, 2021

@Bardiamist
I have pushed a new commit. Could you retest it ?

@Bardiamist
Copy link

@Bardiamist
I have pushed a new commit. Could you retest it ?

LGTM. Tested 5 times without crash. Gesture was fixed.

@troZee
Copy link
Member

troZee commented Aug 23, 2021

@Bardiamist
I have pushed a new commit. Could you retest it ?

LGTM. Tested 5 times without crash. Gesture was fixed.

Thank you for cooperation. Before the merge, I will wait for the other developers.

@troZee
Copy link
Member

troZee commented Aug 23, 2021

This issue has been fixed here: https://github.com/callstack/react-native-pager-view/releases/tag/v5.4.1

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

Successfully merging a pull request may close this issue.