ReactNative源码分析之ReactContext创建流程

当前分析的ReactNative版本为:0.61.5:

ReactNative中最核心的当属ReactContext,想访问ReactNative框架相关类,第一个需要的就是ReactContext。

原生层定义NativeModule, 我们往往直接继承ReactContextBaseJavaModule,而该类构造函数传入的参数就是ReactApplicationContext,再来看看它的定义,原来它的父类就是ReactContext。

既然ReactContext如此重要,那么我们就来看看它是如何创建的。

在介绍之前,我们先介绍ReactInstanceManager,该类核心职责之一就是创建ReactContext,而创建的入口方法就是createReactContextInBackground,代码如下:

  /**
   * Trigger react context initialization asynchronously in a background async task. This enables
   * applications to pre-load the application JS, and execute global code before {@link
   * ReactRootView} is available and measured.
   *
   * <p>Called from UI thread.
   */
  @ThreadConfined(UI)
  public void createReactContextInBackground() {
   
    Log.d(ReactConstants.TAG, "ReactInstanceManager.createReactContextInBackground()");
    UiThreadUtil
        .assertOnUiThread(); // Assert before setting mHasStartedCreatingInitialContext = true
    if (!mHasStartedCreatingInitialContext) {
   
      mHasStartedCreatingInitialContext = true;
      recreateReactContextInBackgroundInner();
    }
  }

该方法是ReactContext创建的入口,接着看recreateReactContextInBackgroundInner方法:

  @ThreadConfined(UI)
  private void recreateReactContextInBackgroundInner() {
   
    Log.d(ReactConstants.TAG, "ReactInstanceManager.recreateReactContextInBackgroundInner()");
    PrinterHolder.getPrinter()
        .logMessage(ReactDebugOverlayTags.RN_CORE, "RNCore: recreateReactContextInBackground");
    UiThreadUtil.assertOnUiThread();

    if (mUseDeveloperSupport && mJSMainModulePath != null) {
   
      final DeveloperSettings devSettings = mDevSupportManager.getDevSettings();

      if (!Systrace.isTracing(TRACE_TAG_REACT_APPS | TRACE_TAG_REACT_JS_VM_CALLS)) {
   
        if (mBundleLoader == null) {
   
          mDevSupportManager.handleReloadJS();
        } else {
   
          mDevSupportManager.isPackagerRunning(
              new PackagerStatusCallback() {
   
                @Override
                public void onPackagerStatusFetched(final boolean packagerIsRunning) {
   
                  UiThreadUtil.runOnUiThread(
                      new Runnable() {
   
                        @Override
                        public void run() {
   
                          if (packagerIsRunning) {
   
                            mDevSupportManager.handleReloadJS();
                          } else if (mDevSupportManager.hasUpToDateJSBundleInCache()
                              && !devSettings.isRemoteJSDebugEnabled()) {
   
                            // If there is a up-to-date bundle downloaded from server,
                            // with remote JS debugging disabled, always use that.
                            onJSBundleLoadedFromServer(null);
                          } else {
   
                            // If dev server is down, disable the remote JS debugging.
                            devSettings.setRemoteJSDebugEnabled(false);
                            recreateReactContextInBackgroundFromBundleLoader();
                          }
                        }
                      });
                }
              });
        }
        return;
      }
    }

    recreateReactContextInBackgroundFromBundleLoader();
  }

接着看recreateReactContextInBackgroundFromBundleLoader方法:

  @ThreadConfined(UI)
  private void recreateReactContextIn
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值