Skip to content

Conversation

@californiandreamer
Copy link

@californiandreamer californiandreamer commented Sep 9, 2025

Summary

  • Adds public iOS API stopReactNative() to deallocate the React Native runtime created by RCTReactNativeFactory and allow clean re‑initialization later. Fixes #134.
  • Old Architecture: calls bridge.invalidate() to tear down JS and native modules.
  • New Architecture: releases the runtime by dropping strong references (no public stop API on RCTHost).
  • Prevents crashes when pushing a React Native screen after stopping.
  • Removes stale lazy caching of rootViewFactory to avoid retaining the old runtime.
  • Documentation updates for the new API and usage:
    • docs/SWIFT.md
    • docs/OBJECTIVE_C.md
  • Backwards compatible: existing startReactNative() API remains; view() now self‑heals if RN was previously stopped.

Test plan

  • Press the “Stop React Native” (to check app won't crash if stopReactNative called before RN was initialize)
  • Push RN screen → pop → tap the “Stop React Native” button → push again (no crash).
  • Repeat with both architectures toggled.
  • Watch the Xcode Instruments graph to confirm RN objects are released after stop (RSS may not drop immediately).

Copy link
Member

@okwasniewski okwasniewski left a comment

Choose a reason for hiding this comment

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

Thanks for working on this! 🚀

Overall looks good, left couple of questions.

cursor[bot]

This comment was marked as outdated.

cursor[bot]

This comment was marked as outdated.

@californiandreamer californiandreamer force-pushed the feat/add-deallocate-method-ios branch from 98b8d14 to 9264071 Compare September 24, 2025 17:44
@californiandreamer californiandreamer force-pushed the feat/add-deallocate-method-ios branch from c11f9c0 to 5bef4b4 Compare September 24, 2025 19:17
@californiandreamer
Copy link
Author

californiandreamer commented Sep 24, 2025

@okwasniewski, I refactored the factory a bit and added some additional checks due to Cursor Bugbot comments.

When it comes to the stopReactNative() call on the RN screen, the app is not crashing but just goes white screen. We can think about a bit better fallback. I spent some time on that and didn't find a solution yet. Will dig deeper 😄

Simulator.Screen.Recording.-.iPhone.16.Pro.-.2025-09-24.at.22.52.53.mp4

@okwasniewski
Copy link
Member

@californiandreamer This looks like a expected behavior if you stop react native, let's ship it.

Comment on lines +141 to 145
if let bridge = reactNativeFactory?.bridge, !bridge.isLoading {
DispatchQueue.main.async { [weak self] in
self?.jsLoaded(Notification(name: notificationName))
}
}
Copy link
Member

Choose a reason for hiding this comment

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

This is changing the previous behavior. jsLoaded should be called by React Native sending this notification

return
}

guard let factory = reactNativeFactory else {
Copy link
Member

Choose a reason for hiding this comment

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

nit: guard let reactNativeFactory else

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.

Add method to deallocate reactNativeFactory instance

2 participants