[英]AsyncStorage reference crashes react native app on iOS
我一直在使用 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
Pods 和我的項目。 如有必要,我可以提供更多調試信息,但不確定在這種情況下什么是相關的。
出於不同的原因,我不想使用 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
時,它在調試模式下工作正常
[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 不是一個對象(評估 '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% 確定這是否能回答您的問題,因為我沒有完全遵循您問題中的所有內容,但這只是我自己的經驗。 如果我能幫助進一步讓我知道。 :)
對於正在閱讀這篇文章並在類似問題上掙扎的人,最終在我頭上的頭發撕裂了三天之后,我們最終完全從本地退出了世博會。 基本上,我們引導了 vanilla without-expo RN 項目,將所有代碼移到那里,一切都神奇地開始工作。
我們假設發生的主要原因是某些本機庫相互沖突並訪問某些 API 導致運行時崩潰,我們已經經歷過這種情況。
好消息是,我們現在有 4 個 RN 應用程序在生產中,整個博覽會的混亂已經過去了。
如果您有興趣從事類似的工作,請通過相同的句柄在一些 twitter 上聯系我。 我們正在用 Bolt 中的 React Native 做很酷的事情。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.