dominicstop / react-native-ios-context-menu

A react-native component to use context menu's (UIMenu) on iOS 13/14+
MIT License
572 stars 29 forks source link

Crash: Exception thrown while executing UI block: This UITargetedPreview initializer requires that the view is in a window, but it is not. #69

Closed danielrhodes closed 1 year ago

danielrhodes commented 1 year ago

I've seen some other people mention things similar to this issue.

A crash occurs when the underlying component that the on-screen native context menu wraps around is unmounted. In this specific case, it is attached to a List cell that got re-rendered. But it applies to any component that gets unmounted.

The primary issue, outside of this being a native level crash, is because these native views are outside the React view hierarchy, they will not go away unless the app is forced closed.

Exception thrown while executing UI block: This UITargetedPreview initializer requires that the view is in a window, but it is not. Either fix that, or use the other initializer that takes a target. (view: <react_native_ios_context_menu.RNIContextMenuView: 0x138219900> => <RCTView: 0x13821c5d0> => <RNFlashList.CellContainer: 0x138207570>)

__44-[RCTUIManager flushUIBlocksWithCompletion:]_block_invoke
    RCTUIManager.m:1202
__44-[RCTUIManager flushUIBlocksWithCompletion:]_block_invoke.206
__RCTExecuteOnMainQueue_block_invoke
_dispatch_call_block_and_release
_dispatch_client_callout
_dispatch_main_queue_drain
_dispatch_main_queue_callback_4CF
__CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__
__CFRunLoopRun
CFRunLoopRunSpecific
GSEventRunModal
-[UIApplication _run]
UIApplicationMain
main
start_sim
0x0
0x0

I've found two workarounds. One is to dismiss the menu on unmount (when the props change).

  useEffect(() => {
    return () => {
      ref.current
        ?.dismissMenu()
        .then(_ => {})
        .catch(_ => {});
    };
  }, [props]);

The other, which seems more preferable (for my use case where the context is attached to a List item):

is

<ContextMenuView
     ref={ref}
     internalCleanupMode={'viewController'}
     shouldEnableAggressiveCleanup={true}
      ...
>
dominicstop commented 1 year ago

Hi @danielrhodes — could you please a make a small repro so i can debug it?
I think i'm calling the internal view cleanup routine too early, or might be re-using the stale views — but i can't seem to repro it (it's been a while since i've looked at the code for this project agajksgdjkasfkhsgl).
I'll add the repro you provide for future bug testing in the example app

dominicstop commented 1 year ago

hi, sorry for the late reply - i think i might need to re-think how the cleanup routine get triggered

dominicstop commented 1 year ago

I released a new version of this library that uses ✨ expo-modules ✨ (pleas see: Installation - Expo Modules Re-Write) asdasfjkdsfjl
I updated a new mode to internalCleanupMode called didMoveToWindowNil, and also added shouldCleanupOnComponentWillUnmountForAuxPreview + shouldCleanupOnComponentWillUnmountForMenuPreview props.
In the meantime, i will continue to do more testing; if it's still an issue w/ the newer version, please tag me or re-open this issue asdfghjklvdfkbjl
thanks 💖