模擬狀況:合併增量後jsbundle文件出現部分錯誤
調試發現當加載jsbundle出現異常時,RN模塊RCTBatchedBridge.m中以下代碼會執行:post
- (void)stopLoadingWithError:(NSError *)error { RCTAssertMainThread(); if (!self.isValid || !self.loading) { return; } _loading = NO; [[NSNotificationCenter defaultCenter] postNotificationName:RCTJavaScriptDidFailToLoadNotification object:_parentBridge userInfo:@{@"bridge": self, @"error": error}]; RCTFatal(error); }
所以native模塊加入監聽處理RCTJavaScriptDidFailToLoadNotification通知的方法便可:測試
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(backToPreVersion) name:RCTJavaScriptDidFailToLoadNotification object:nil]; - (BOOL)backToPreVersion { // rollback }
模擬狀況:若是RN代碼存在Crash bug
定位代碼Crash時代碼會執行到RCTAssert.m中以下語句:ui
void RCTFatal(NSError *error) { _RCTLogNativeInternal(RCTLogLevelFatal, NULL, 0, @"%@", [error localizedDescription]); RCTFatalHandler fatalHandler = RCTGetFatalHandler(); if (fatalHandler) { fatalHandler(error); } else { #if DEBUG @try { #endif NSString *name = [NSString stringWithFormat:@"%@: %@", RCTFatalExceptionName, [error localizedDescription]]; NSString *message = RCTFormatError([error localizedDescription], error.userInfo[RCTJSStackTraceKey], 75); [NSException raise:name format:@"%@", message]; #if DEBUG } @catch (NSException *e) { } #endif } }
測試DEBUG模式下RN拋出異常後被catch,不會致使Crash,所以爲保證RELEASE版本下程序不會Crash,依舊能更新增量包,由於能夠將#if DEBUG的選項去掉,不讓程序Crash。spa