我一直在使用 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/