问题描述
我将Firebase集成到了我的Swift应用程序中.所有初始化完成都是通过编程方式进行的,并且FirebaseApp.config()
在AppDelegate中的didLaunchWithOptions
中被称为第一件事.
I have a Firebase integrated to my swift app. All the initialisation done is programmatically and the FirebaseApp.config()
is called the very first thing in didLaunchWithOptions
in the AppDelegate.
但是出于某种随机原因,只要应用长时间在后台运行,该应用就会在运行时崩溃,即使其处于活动状态.有趣的是,此崩溃在以下流程中恰好发生了2次:
But for some random reason, whenever the app is in the background for a long time, the app just crashes on the run, i.e making it's state active.Interestingly, this crash happens exactly 2 times in the following flow:
1. The App is idle in background
2. You bring it to the active mode(You just tap the icon to open the app. All this while the app isn't killed)
3. Tap 1 ---- The App Crashes----
4. Tap 2 ---- The App Crashes----
5. Tap 3 ---- The App Runs Normally ----
我试图进行故障排除,但是我所有的努力都徒劳无功.我尝试同时设置情节提要和以编程方式启动应用程序.什么都行不通.
I tried to troubleshoot but all my efforts have gone in vain. I have tried setting up both the storyboards and programmatically launching app. Nothing works.
以下是堆栈跟踪:
SpotMi-Dev[74741:2958059] *** Terminating app due to uncaught exception 'FIRAppNotConfigured', reason: 'Failed to get default Firebase Database instance. Must call `[FIRApp configure]` (`FirebaseApp.configure()` in Swift) before using Firebase Database.'
*** First throw call stack:
(
0 CoreFoundation 0x000000010d3816fb __exceptionPreprocess + 331
1 libobjc.A.dylib 0x000000010c14aac5 objc_exception_throw + 48
2 CoreFoundation 0x000000010d381555 +[NSException raise:format:] + 197
3 SpotMi-Dev 0x0000000101be5ba1 +[FIRDatabase database] + 97
4 SpotMi-Dev 0x0000000101aaba6b $s10SpotMi_Dev13FBDataserviceCACycfc + 59
5 SpotMi-Dev 0x0000000101aac193 $s10SpotMi_Dev13FBDataserviceCACycfcTo + 19
6 SpotMi-Dev 0x0000000101aa696c $s10SpotMi_Dev13FBDataserviceCACycfC + 44
7 SpotMi-Dev 0x0000000101aa692c globalinit_33_8263DB418F9D5BC81149F56895386F46_func0 + 28
8 libdispatch.dylib 0x000000010ecaddb5 _dispatch_client_callout + 8
9 libdispatch.dylib 0x000000010ecaf83d _dispatch_once_callout + 66
10 libswiftCore.dylib 0x000000010e2c6579 swift_once + 25
11 SpotMi-Dev 0x0000000101aa69f4 $s10SpotMi_Dev13FBDataserviceC2dsACvau + 36
12 SpotMi-Dev 0x000000010180b013 $s10SpotMi_Dev9ProfileVCC18initializeUserPostyyF + 179
13 SpotMi-Dev 0x0000000101809278 $s10SpotMi_Dev9ProfileVCC11viewDidLoadyyF + 616
14 SpotMi-Dev 0x00000001018095b4 $s10SpotMi_Dev9ProfileVCC11viewDidLoadyyFTo + 36
15 UIKitCore 0x000000011589443b -[UIViewController loadViewIfRequired] + 1183
16 UIKitCore 0x0000000115894868 -[UIViewController view] + 27
17 UIKitCore 0x0000000115ebd4ad -[UIApplication(StateRestoration) _restoreApplicationPreservationStateWithSessionIdentifier:beginHandler:completionHandler:] + 6413
18 UIKitCore 0x0000000115eb8fdc -[UIApplication(StateRestoration) _doRestorationIfNecessary] + 211
19 UIKitCore 0x0000000115e8e2c9 -[UIApplication _handleDelegateCallbacksWithOptions:isSuspended:restoreState:] + 208
20 UIKitCore 0x0000000115e8fcad -[UIApplication _callInitializationDelegatesForMainScene:transitionContext:] + 3932
21 UIKitCore 0x0000000115e950c6 -[UIApplication _runWithMainScene:transitionContext:completion:] + 1617
22 UIKitCore 0x00000001156da6d6 __111-[__UICanvasLifecycleMonitor_Compatability _scheduleFirstCommitForScene:transition:firstActivation:completion:]_block_invoke + 904
23 UIKitCore 0x00000001156e2fce +[_UICanvas _enqueuePostSettingUpdateTransactionBlock:] + 153
24 UIKitCore 0x00000001156da2ec -[__UICanvasLifecycleMonitor_Compatability _scheduleFirstCommitForScene:transition:firstActivation:completion:] + 236
25 UIKitCore 0x00000001156dac48 -[__UICanvasLifecycleMonitor_Compatability activateEventsOnly:withContext:completion:] + 1091
26 UIKitCore 0x00000001156d8fba __82-[_UIApplicationCanvas _transitionLifecycleStateWithTransitionContext:completion:]_block_invoke + 782
27 UIKitCore 0x00000001156d8c71 -[_UIApplicationCanvas _transitionLifecycleStateWithTransitionContext:completion:] + 433
28 UIKitCore 0x00000001156dd9b6 __125-[_UICanvasLifecycleSettingsDiffAction performActionsForCanvas:withUpdatedScene:settingsDiff:fromSettings:transitionContext:]_block_invoke + 576
29 UIKitCore 0x00000001156de610 _performActionsWithDelayForTransitionContext + 100
30 UIKitCore 0x00000001156dd71d -[_UICanvasLifecycleSettingsDiffAction performActionsForCanvas:withUpdatedScene:settingsDiff:fromSettings:transitionContext:] + 223
31 UIKitCore 0x00000001156e26d0 -[_UICanvas scene:didUpdateWithDiff:transitionContext:completion:] + 392
32 UIKitCore 0x0000000115e939a8 -[UIApplication workspace:didCreateScene:withTransitionContext:completion:] + 514
33 UIKitCore 0x0000000115a4adfa -[UIApplicationSceneClientAgent scene:didInitializeWithEvent:completion:] + 361
34 FrontBoardServices 0x00000001133a4125 -[FBSSceneImpl _didCreateWithTransitionContext:completion:] + 448
35 FrontBoardServices 0x00000001133aded6 __56-[FBSWorkspace client:handleCreateScene:withCompletion:]_block_invoke_2 + 283
36 FrontBoardServices 0x00000001133ad700 __40-[FBSWorkspace _performDelegateCallOut:]_block_invoke + 53
37 libdispatch.dylib 0x000000010ecaddb5 _dispatch_client_callout + 8
38 libdispatch.dylib 0x000000010ecb12ba _dispatch_block_invoke_direct + 300
39 FrontBoardServices 0x00000001133df146 __FBSSERIALQUEUE_IS_CALLING_OUT_TO_A_BLOCK__ + 30
40 FrontBoardServices 0x00000001133dedfe -[FBSSerialQueue _performNext] + 451
41 FrontBoardServices 0x00000001133df393 -[FBSSerialQueue _performNextFromRunLoopSource] + 42
42 CoreFoundation 0x000000010d2e8be1 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
43 CoreFoundation 0x000000010d2e8463 __CFRunLoopDoSources0 + 243
44 CoreFoundation 0x000000010d2e2b1f __CFRunLoopRun + 1231
45 CoreFoundation 0x000000010d2e2302 CFRunLoopRunSpecific + 626
46 GraphicsServices 0x000000011034e2fe GSEventRunModal + 65
47 UIKitCore 0x0000000115e96ba2 UIApplicationMain + 140
48 SpotMi-Dev 0x000000010174287b main + 75
49 libdyld.dylib 0x000000010ed22541 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
以下是didLaunchWithOptions中的代码:
Following is the code from didLaunchWithOptions:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
#if DEVELOPMENT
print("Development Mode Started")
#else
print("Production Mode Started")
#endif
FirebaseApp.configure()
Messaging.messaging().delegate = self
//other app functions
return true
}
我正在初始化控制器的viewDidLoad中调用Firebase API.这是通过它调用的服务,甚至还包含崩溃.
I am calling the Firebase API in the viewDidLoad of init controller. And this is the Service through which it gets called and this even contains the crash aswell.
class FBDataservice : NSObject {
static var ds = FBDataservice() //<------ Creates Error here on every single crash related to this issue
let DB_URL: DatabaseReference = Database.database().reference()
let ST_URL: StorageReference = Storage.storage().reference()
private lazy var _REF_BASE = DB_URL
}
然后我只需在viewDidLoad或viewDidAppear中调用FBDataservice.ds._REF_BASE.child("something").observe( .Event, with (Datasnapshot))
.我只是在获取数据.然后在AppDelegate的didBecameActive
上推送数据,例如更新应用程序RunCounts
And then I just call FBDataservice.ds._REF_BASE.child("something").observe( .Event, with (Datasnapshot))
in the viewDidLoad or viewDidAppear. I am just mostly fetching the data. And then push data like updating app RunCounts on didBecameActive
in AppDelegate
任何帮助将不胜感激.干杯
Any help will immensely be appreciated. Cheers
推荐答案
检查FirebaseApp.app()
是否为nil
您可以再次调用FirebaseApp.configure()
吗?如果在appDidBecomeActive:
或didEneterInForeground:
方法中崩溃,则需要进行调试.
Check FirebaseApp.app()
if this is nil
can you call the FirebaseApp.configure()
again? You need to debug if this crashes in appDidBecomeActive:
or didEneterInForeground:
method.
还请在启动应用程序后(配置行除外)在第一次检查您在哪里使用FirebaseDatabase
?我的意思是在任何ViewController
或任何地方,我们都需要在此处检查情况.
Also please check where you use FirebaseDatabase
at very first time after launching the application except the configuration line? I mean is in any ViewController
or anywhere, we need to check the condition here.
更新的问题:
关于更新的问题FBDataservice()
,您在哪里创建它的实例,因为在创建它的实例时,它将创建与Firebase相关的DB_URL
和ST_URL
url实例,因此在此之前可能未配置Firebase
.您可以重写其init方法来检查是否配置了Firebase
.
Regarding your updated question FBDataservice()
where are you making its instance because as you create its instance it will create DB_URL
and ST_URL
url instances which are related to Firebase so probably Firebase
is not configured before this. You can override its init method to check if Firebase
is configured or not.
您获取了两个变量,每当您要创建此类的实例时,这些变量都将在构造函数中创建.
You took two variables which will be create in constructor whenever you will make an instance of this class.
尝试一件事...替换
static var ds = FBDataservice()
与
static var ds: FBDataservice {
if FirebaseApp.app() == nil {
FirebaseApp.configure()
}
return FBDataservice()
}
这篇关于从后台激活后,Firebase App在启动时崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!