Firebase 应用程序在从后台激活时在启动时崩溃

Posted

技术标签:

【中文标题】Firebase 应用程序在从后台激活时在启动时崩溃【英文标题】:Firebase App crashed on launch when made active from background 【发布时间】:2019-05-27 11:13:10 【问题描述】:

我的 swift 应用程序中集成了一个 Firebase。所有的初始化都是以编程方式完成的,FirebaseApp.config() 在 AppDelegate 的didLaunchWithOptions 中被称为第一件事。

但由于某种随机原因,每当应用程序长时间处于后台时,应用程序就会在运行时崩溃,即使其处于活动状态。 有趣的是,这种崩溃在以下流程中恰好发生了 2 次:

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 ----

我试图排除故障,但我所有的努力都白费了。我尝试设置故事板和以编程方式启动应用程序。没有任何效果。

以下是堆栈跟踪:

            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 的代码:

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

我在 init 控制器的 viewDidLoad 中调用 Firebase API。这是调用它的服务,这甚至包含崩溃

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 上更新 app RunCounts

我们将不胜感激任何帮助。干杯

【问题讨论】:

评论不适用于扩展讨论或调试会话;这个对话是archived in chat。 【参考方案1】:

检查FirebaseApp.app(),如果这是nil,你能再打电话给FirebaseApp.configure()吗?如果这在appDidBecomeActive:didEneterInForeground: 方法中崩溃,您需要调试。

另外请检查您在启动应用程序后第一次使用FirebaseDatabase 的位置(除了配置行)?我的意思是在任何ViewController 或任何地方,我们需要在这里检查条件。

更新问题:

关于您更新的问题FBDataservice(),您在哪里创建它的实例,因为当您创建它的实例时,它将创建与 Firebase 相关的 DB_URLST_URL url 实例,因此在此之前可能未配置 Firebase。你可以重写它的init方法来检查Firebase是否被配置。

当您创建此类的实例时,您将在构造函数中创建两个变量。

尝试一件事...替换

static var ds = FBDataservice() 

static var ds: FBDataservice  
    if FirebaseApp.app() == nil  
         FirebaseApp.configure() 
     
    return FBDataservice() 

【讨论】:

嘿,我刚刚遇到另一个错误。 Terminating app due to uncaught exception 'com.firebase.core', reason: 'Default app has already been configured.'我想因为 Config 在 didFinishWithLaunch 中被调用两次,然后在实例中。您建议删除 appdelegate 中的那个吗? 我刚刚在委托中添加了相同的 FirebaseApp.app() 检查。所以解决了。但是你还有什么建议吗? 尝试在AppDelegate 中也加入相同的条件。 if FirebaseApp.app() == nil FirebaseApp.configure()

以上是关于Firebase 应用程序在从后台激活时在启动时崩溃的主要内容,如果未能解决你的问题,请参考以下文章

在启动时在iOS中启动位置感知后台服务

启动 Core Data 应用程序时在后台配置 NSFetchedResultsController

在应用程序启动时在后台加载 UIViews

从后台恢复显示 ModalViewController,避免下面的内容闪烁

如何在启动时在后台运行django服务器?

如何在从通知(活动2)启动的活动后启动MainActivity(活动1)在后台销毁