ios - AsyncStorage 引用崩溃 react iOS 上的 native 应用程序

标签 ios react-native expo

我一直在使用 ExpoKit 开发一个应用程序,该应用程序依赖于 AsyncStorage 的使用,并且一切正常,直到我想将它部署到 prod。

在我的 package.json我有:"react-native": "https://github.com/expo/react-native/archive/sdk-30.0.0.tar.gz"iOS Deployment Target设置为 10.0对于 Pod 和我的项目。
如有必要,我可以提供更多调试信息,但不确定在这种情况下什么是相关的。

由于不同的原因,我不想使用 ExpoKit 在生产应用程序中托管捆绑包的默认方式,但仍想保留 expo 的开发工具,因此我更改了 AppDelegate.m 的代码对此:

#import "AppDelegate.h"
#import "ExpoKit.h"
#import "EXViewController.h"
#import <React/RCTBundleURLProvider.h>
#import <React/RCTRootView.h>

@interface AppDelegate ()

@property (nonatomic, strong) EXViewController *rootViewControllerExpo;

@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    #ifdef DEBUG
        // Expo
        [[ExpoKit sharedInstance] application:application didFinishLaunchingWithOptions:launchOptions];
        _rootViewControllerExpo = [ExpoKit sharedInstance].rootViewController;

        _window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
        _window.backgroundColor = [UIColor whiteColor];
        _window.rootViewController = _rootViewControllerExpo;
        [_window makeKeyAndVisible];
    #else
        // React Native
        NSURL *jsCodeLocation;
        jsCodeLocation = [[NSBundle mainBundle] URLForResource:@"main" withExtension:@"jsbundle"];

        RCTRootView *rootView = [[RCTRootView alloc] initWithBundleURL:jsCodeLocation
                                                            moduleName:@"RentalCollectorsApp"
                                                     initialProperties:nil
                                                         launchOptions:launchOptions];

        UIViewController *rootViewController = [UIViewController new];
        rootViewController.view = rootView;

        _window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
        _window.backgroundColor = [UIColor whiteColor];
        _window.rootViewController = rootViewController;
        [_window makeKeyAndVisible];

        //        UIView* launchScreenView = [[[NSBundle mainBundle] loadNibNamed:@"LaunchScreen" owner:self options:nil] objectAtIndex:0];
        //        launchScreenView.frame = self.window.bounds;
        //        rootView.loadingView = launchScreenView;
    #endif

    return YES;
}

然后我的应用程序在使用 ExpoKit 时运行良好,但在执行发布方案并显示以下消息时,它在打开时开始崩溃。使用 Expo 的 rootViewController 时,它在 Debug模式下工作正常
[error][tid:com.facebook.react.JavaScript] undefined is not an object (evaluating 'l.multiMerge')
[fatal][tid:com.facebook.react.ExceptionsManagerQueue] Unhandled JS Exception: undefined is not an object (evaluating 'l.multiMerge')
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

经过数小时的调试,我已经确定了导致崩溃的最小情况。单独导入 AsyncStorage 不会使应用程序崩溃,但在变量中引用它会立即导致崩溃:
import React, { Component } from 'react';
import { Text, View, AsyncStorage } from 'react-native';

export default class AppContainer extends Component {
  render() {
    const bar = AsyncStorage;
    // const bar = {
    //   storage: AsyncStorage,
    // };

    return (
      <View
        style={{
          flex: 1,
          justifyContent: 'center',
          alignItems: 'center',
          backgroundColor: 'yellow',
        }}
      >
        <Text>Hello!</Text>
      </View>
    );
  }
}

在我的特殊情况下,崩溃是由使用 redux-persist 引起的。 ,但似乎问题更广泛,因为当我删除 redux-persist 的初始化时它开始在 react-native-mapbox-gl 中间的某个地方崩溃.仅供引用,代码redux-persist https://github.com/rt2zz/redux-persist/blob/master/src/storage/index.native.js

谷歌搜索给了我一些结果,但没有答案:

Undefined is not an object (evaluating 'RCTAsyncStorage.multiMerge' (React-native-asyncstorage)

https://github.com/facebook/react-native/issues/21948

任何帮助表示赞赏。

最佳答案

您不应该将 AsyncStorage 直接分配给任何变量。如果用户启用缓存,要在 AsyncStorage 中设置项目,请执行以下操作:

import AsyncStorage from 'react-native';
const persistTest = {
   AsyncStorage
};

persistTest.AsyncStorage.setItem("testCache", "Y");

并检索并继续:
persistTest.AsyncStorage.getItem("testCache").then(res => {
  this.continueTest(res);
});

我不能 100% 确定这是否回答了您的问题,因为我没有完全按照您的问题进行操作,但这只是根据我自己的经验。如果我可以帮助进一步让我知道。 :)

关于ios - AsyncStorage 引用崩溃 react iOS 上的 native 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53265915/

相关文章:

ios - 如何在后台获取通知和数据同步

ios - Mail Composer 不会关闭 iOS

ios - React-Native 无法连接到开发服务器

ios - 世博套件 : "Native Module cannot be null" error after installing expo-in-app-purchase

android - 无法通过隧道在物理设备上运行我的 native 应用程序

expo - 使用 expo 发布 IOS 和 Android 独立应用程序的更新

ios - 如何创建 STPCard Stripe ios?

iphone - 是否可以重新定位和调整标准 UITableViewCell 的 UIImageView 的大小

javascript - npx react-native 链接命令在最新版本的 react native 中不起作用。我们如何在我的项目中链接自定义字体系列

android - 为什么集成React Native时compileSdkVersion错误?