原生模块生命周期
¥Native Modules Lifecycle
在 React Native 中,原生模块是单例的。原生模块基础结构会在首次访问时延迟创建原生模块,并在应用需要时将其保留。这是一项性能优化,它使我们能够避免在应用启动时预创建原生模块的开销,并确保更快的启动时间。
¥In React Native, Native Modules are singleton. The Native Module infrastructure lazily creates a Native Modules the first time it is accessed and it keeps it around whenever the app requires it. This is a performance optimization that allows us to avoid the overhead of creating Native Modules eagerly, at app start, and it ensure faster startup times.
在纯 React Native 应用中,原生模块只会创建一次,并且不会被销毁。但是,在更复杂的应用中,可能会出现原生模块被销毁并重新创建的用例。例如,想象一下一个棕地应用,它混合了一些原生视图和一些 React Native 界面,如 与现有应用集成指南 中所示。在这种情况下,当用户离开 React Native 界面时销毁 React Native 实例,并在用户返回该界面时重新创建该实例,这可能是合理的。
¥In a pure React Native app, the Native Modules are created once and they are never destroyed. However, in more complex apps, there might be use cases where the Native Modules are destroyed and recreated. Imagine, for example, a brownfield app that mixes some native views with some React Native surfaces, as presented in the Integrating with Existing App guide. In that case it might make sense to destroy a React Native instance when the user navigates away from a React Native surface and recreate it when the user navigates back to that surface.
发生这种情况时,无状态的原生模块不会引起任何问题。但是,对于有状态的原生模块,可能需要正确地使原生模块无效,以确保重置状态并释放资源。
¥When this happens, Native Module that are stateless won't cause any issues. However, for stateful Native Modules it might be necessary to properly invalidate the Native Module to ensure that the state is reset and the resources released.
在本指南中,你将探索如何正确地初始化和使原生模块失效。本指南假设你熟悉如何编写原生模块,并且能够轻松地编写原生代码。如果你不熟悉原生模块,请先阅读 原生模块指南。
¥In this guide, you will explore how to initialize and invalidate a Native Module properly. This guide assumes that you are familiar with how to write a Native Modules and you are comfortable writing native code. If you are not familiar with Native Modules, please read the Native Modules guide first.
安卓
¥Android
对于 Android,所有原生模块都已经实现了 TurboModule 接口,该接口定义了两个方法:initialize()
和 invalidate()
。
¥When it comes to Android, all the Native Modules already implements a TurboModule interface that defines two methods: initialize()
and invalidate()
.
在创建 Native 模块时,原生模块基础架构会调用 initialize()
方法。这是放置所有需要访问 ReactApplicationContext 的初始化代码的最佳位置。以下是一些来自核心的实现了 initialize()
方法的原生模块:BlobModule, NetworkingModule.
¥The initialize()
method is called by the Native Module infrastructure when the Native Module is created. This is the best place to put all the initialization code that needs access to the ReactApplicationContext, for example. These are some Native Modules from core that implements the initialize()
method: BlobModule, NetworkingModule.
在销毁 Native 模块时,原生模块基础架构会调用 invalidate()
方法。这是放置所有清理代码的最佳位置,这些代码会重置原生模块状态并释放不再需要的资源,例如内存和文件。以下是一些来自核心的实现了 invalidate()
方法的原生模块:DeviceInfoModule、NetworkModule
¥The invalidate()
method is called by the Native Module infrastructure when the Native Module is destroyed. This is the best place to put all the cleanup code, resetting the Native Module state and release resources that are no longer needed, such as memory and files. These are some Native Modules from core that implements the invalidate()
method: DeviceInfoModule, NetworkModule
iOS
在 iOS 上,原生模块符合 RCTTurboModule
协议。但是,此协议不会公开 Android TurboModule
类公开的 initialize
和 invalidate
方法。
¥On iOS, Native Modules conforms to the RCTTurboModule
protocol. However, this protocol does not expose the initialize
and invalidate
method that are exposed by the Android's TurboModule
class.
相反,在 iOS 上,有两个附加协议:RCTInitializing
和 RCTInvalidating
。这些协议分别用于定义 initialize
和 invalidate
方法。
¥Instead, on iOS, there are two additional protocols: RCTInitializing
and RCTInvalidating
. These protocols are used to define the initialize
and invalidate
methods, respectively.
如果你的模块需要运行一些初始化代码,那么你可以遵循 RCTInitializing
协议并实现 initialize
方法。为此,你必须:
¥If your module needs to run some initialization code, then you can conform to the RCTInitializing
protocol and implement the initialize
method. To do so, you have to:
-
通过添加以下行修改
NativeModule.h
文件:¥Moduify the
NativeModule.h
file by adding the following lines:
+ #import <React/RCTInitializing.h>
//...
- @interface NativeModule : NSObject <NativeModuleSpec>
+ @interface NativeModule : NSObject <NativeModuleSpec, RCTInitializing>
//...
@end
-
在
NativeModule.mm
文件中实现initialize
方法:¥Implement the
initialize
method in theNativeModule.mm
file:
// ...
@implementation NativeModule
+- (void)initialize {
+ // add the initialization code here
+}
@end
以下是一些来自核心的实现了 initialize
方法的原生模块:RCTBlobManager, RCTTiming.
¥These are some Native Modules from core that implements the initialize
method: RCTBlobManager, RCTTiming.
如果你的模块需要运行一些清理代码,那么你可以遵循 RCTInvalidating
协议并实现 invalidate
方法。为此,你必须:
¥If your module needs to run some cleanup code, then you can conform to the RCTInvalidating
protocol and implement the invalidate
method. To do so, you have to:
-
通过添加以下行修改
NativeModule.h
文件:¥Moduify the
NativeModule.h
file by adding the following lines:
+ #import <React/RCTInvalidating.h>
//...
- @interface NativeModule : NSObject <NativeModuleSpec>
+ @interface NativeModule : NSObject <NativeModuleSpec, RCTInvalidating>
//...
@end
-
在
NativeModule.mm
文件中实现invalidate
方法:¥Implement the
invalidate
method in theNativeModule.mm
file:
// ...
@implementation NativeModule
+- (void)invalidate {
+ // add the cleanup code here
+}
@end
以下是一些来自核心的实现了 invalidate
方法的原生模块:RCTAppearance, RCTDeviceInfo.
¥These are some Native Modules from core that implements the invalidate
method: RCTAppearance, RCTDeviceInfo.