Aakash Dave
Aakash Dave

Reputation: 896

Firebase App crashed on launch when made active from background

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.

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.

Following is the stack trace :

            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

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
}

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

}

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

Upvotes: 2

Views: 3095

Answers (1)

TheTiger
TheTiger

Reputation: 13354

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.

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.

Updated Question:

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.

Try one thing... replace

static var ds = FBDataservice() 

with

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

Upvotes: 1

Related Questions