反应本机应用程序上下文

时间:2020-06-18 06:21:54

标签: android react-native

1a。使用React Native版本0.59.10和react-native-navigation版本1.1.493开发应用程序。我使用MobX进行状态管理。

当前,我正在尝试集成一个可跟踪用户驾驶会话的本地SDK。

在可以初始化SDK之前,先询问用户位置权限。

SDK初始化后,将使用以下代码向JS层广播事件:

fun notifyJS(
  context: Context?,
  receiver: BroadcastReceiver,
  eventName: String,
  data: WritableMap?
) {
  val reactApplication = context?.applicationContext as? ReactApplication
  val instanceManager = reactApplication?.reactNativeHost?.reactInstanceManager
  val reactContext = instanceManager?.currentReactContext
  if (reactContext != null) {
    Log.d(
      LOG_TAG,
      "React context is available, sending event $eventName"
    )
    reactApplication.reactNativeHost?.reactInstanceManager?.currentReactContext?.getJSModule(
      DeviceEventManagerModule.RCTDeviceEventEmitter::class.java
    )?.emit(eventName, data)
  } else {
    Log.d(LOG_TAG, "React context is not available")
    val pendingResult = receiver.goAsync()
    instanceManager!!.addReactInstanceEventListener(
      object : ReactInstanceManager.ReactInstanceEventListener {
        override fun onReactContextInitialized(reactContext: ReactContext) {
          Log.v(
            LOG_TAG,
            "React context is initialized sending js event $eventName"
          )
          reactContext.getJSModule(
            DeviceEventManagerModule.RCTDeviceEventEmitter::class.java
          )?.emit(eventName, data)
          instanceManager.removeReactInstanceEventListener(this)
          pendingResult.finish()
        }
      })
    instanceManager.createReactContextInBackground()
    Log.d(LOG_TAG, "Called to create context in the background")
  }
}

这时,即使应用程序位于前台,导致应用程序自行重启,reactContext变量也始终为null。

我对本机开发有基本的了解,我在网上搜索了数小时的案例,但似乎无法理解当应用程序处于前台时上下文如何为空。

所以我猜测可能是这段代码获取了错误的上下文...?

我的应用程序使用其他一些第三方库,这是当前的package.json文件

"dependencies": {
    "@appdynamics/react-native-agent": "^1.0.223",
    "@types/react": "^16.9.1",
    "@types/react-native": "^0.57.65",
    "babel-plugin-macros": "^2.6.1",
    "bugsnag-react-native": "^2.19.1",
    "cross-env": "^6.0.3",
    "fsevents": "^2.1.3",
    "intent-router": "0.0.13",
    "mobx": "3.3.1",
    "mobx-react": "4.3.3",
    "moment": "^2.24.0",
    "moment-timezone": "^0.5.26",
    "react": "16.8.3",
    "react-native": "0.59.10",
    "react-native-audio": "^4.1.3",
    "react-native-base64": "^0.0.2",
    "react-native-cardview": "^1.1.5",
    "react-native-chart-kit": "^5.5.0",
    "react-native-circular-progress": "^1.3.6",
    "react-native-communications": "^2.2.1",
    "react-native-config": "^0.11.7",
    "react-native-device-info": "^2.3.2",
    "react-native-doc-viewer": "^2.7.8",
    "react-native-document-picker": "^3.2.4",
    "react-native-fetch-blob": "^0.10.8",
    "react-native-firebase": "^5.5.6",
    "react-native-image-picker": "^0.26.7",
    "react-native-maps": "0.25.0",
    "react-native-mime-types": "^2.2.1",
    "react-native-navigation": "1.1.493",
    "react-native-open-settings": "^1.0.1",
    "react-native-pages": "^0.9.0",
    "react-native-render-html": "^4.1.2",
    "react-native-restart": "^0.0.12",
    "react-native-settings": "^0.1.1",
    "react-native-signature-capture": "^0.4.9",
    "react-native-snackbar": "^2.2.0",
    "react-native-sound": "^0.10.9",
    "react-native-splash-screen": "^3.2.0",
    "react-native-svg": "^12.1.0",
    "react-native-swipe-list-view": "^3.0.1",
    "react-native-swipeable": "^0.6.0",
    "react-native-swiper": "^1.5.14",
    "react-native-timer": "^1.3.6",
    "react-native-zendrive": "latest",
    "typescript": "^3.5.3",
    "victory-native": "^34.1.0"
  }

在进行测试以找出导致此问题的原因后,我开始从代码中删除库,即使仅将以下代码加载:ReactNativePackage,ReactNativeConfigPackage和RNNPackage也会发生,所以我很确定问题出在使用react本地导航。

因此,我尝试创建一个新的黑色应用程序来重现此问题,但是在全新的应用程序(相同的react-native和rnn版本)上,该问题无法重现。因此,我猜测随着时间的推移,在开发应用程序时,一些配置和补丁会放置在代码中。

我真的很难调试和定位导致此问题的原因

0 个答案:

没有答案