53

I'm trying to upgrade my app to the new version of Firebase. I went through the setup guide and edited all of my code to match the new syntax. However, when I run the app, I get these two errors.

The default app has not been configured yet.
Terminating app due to uncaught exception 'MissingDatabaseURL', reason: 'Failed to get FIRDatabase instance: FIRApp object has no databaseURL in its FirebaseOptions object.'

I have FIRApp.configure() in the AppDelegate and I have the GoogleServices-Info.plist imported into my project. The plist has all of the correct info as well. Anyone else running into this or know how to fix it?

1
  • Have you tried looking at some of these answers?
    – James
    Commented May 19, 2016 at 21:34

10 Answers 10

87

Here's the answer to your problem:

To configure Firebase you have to execute FIRApp.configure() somewhere. After this is done you can use let firebaseDatabaseReference = FIRDatabase.database().reference() to get a reference to that database and start using it. The problem isn't with Firebase "per se" but with how Swift behaves.

If you put FIRApp.configure() in your AppDelegate func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool and then in the MyDatabase class you use let firebaseDatabaseReference = FIRDatabase.database().reference() outside of your declared functions sometimes the code FIRDatabase.database().reference() executes before the application didFinishLaunchingWithOptions function is executed.

Essentially your class is trying to get a reference to the Firebase database before it has a chance to configure itself, generating the error in the console "The default app has not been configured yet."

Note: This doesn't happen all the time, sometimes the application is slow to start, in iOS Simulator for example, and it doesn't have a chance to finish before MyDatabase "let" executes and tries to get a reference.

That is why moving the FIRApp.configure() code to override init() in AppDelegate works, essentially it makes sure the configure code gets executed when AppDelegate is initialised (in this and most cases, before MyDatabase is initialised)

override init() {
   super.init()
   FIRApp.configure()
   // not really needed unless you really need it FIRDatabase.database().persistenceEnabled = true
}

Also make sure you super.init() (so you super classes get the "message") so you override doesn't do more harm than good.

10
  • 3
    I solved it by moving my ref = FIRDatabase.database().reference() inside viewDidLoad method of my UIViewController, and declared var ref : FIRDatabaseReference!. Are there any downsides to this, or is there a reason to prefer overriding the init() of AppDelegate?
    – Vlad V
    Commented Mar 17, 2017 at 20:09
  • You don't necessarily have to move it to the init() function, just make sure that you don't have any other globally accessible structs/classes, etc accessing the Firebase objects before its configured.
    – Bill
    Commented May 29, 2017 at 20:23
  • 1
    your explanation makes me clear, thanks its works perfect for me.
    – Pankaj K.
    Commented Oct 6, 2017 at 12:04
  • I have globals accessing Firebase objects ie let db = Firestore.firestore() because I can guarantee that it is not nil. If I wait until onViewDidLoad to set it equal then I've got to turn them into optionals, which I like to avoid when I can. Commented Apr 14, 2018 at 21:39
  • 1
    I don't know why, but doing this disables app delegate swizzling. So if you are using Phone Authentication that relies on app delegate swizzling, then you might not want to put the configure function in init.
    – WYS
    Commented Sep 5, 2018 at 13:52
44

I'm also using Fabric and in my case it was the order of Fabric and Firebase initializations. I had to initialize Firebase first.

So changing

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    Fabric.with([Crashlytics.self])
    FirebaseApp.configure()
    ...
}

to:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    FirebaseApp.configure()
    Fabric.with([Crashlytics.self])
    ...
}

fixed the problem.

0
4

In AppDelegate.m, outside of didFinishLaunchingWithOptions,

override init() {
   FIRApp.configure()
   FIRDatabase.database().persistenceEnabled = true
}
1
  • Did you have the persistenceEnabled call there the whole time? That being called first will have triggered the FIRDatabase class to to check whether it was configured, and fail. You can move the configure back to where it was, as long as you move the persistenceEnabled flag with it.
    – Ian Barber
    Commented May 19, 2016 at 22:11
2

Make sure you are having DATABASE_URL key in your GoogleService-Info.plist

1
  • This was the problem for me. Downloaded it again and this key-value pair was present. Now a new problem occurred, and it seems the SDK doesn't know what to do with database locations outside of the US. I used Belgium, and the SDK couldn't parse the URL. I used USA and it worked.
    – horseshoe7
    Commented Jan 21, 2021 at 13:07
2

Swift 5 - Easy Solution

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

return true
}


//MARK:- This function will auto run and firebase will configure successfully
override init() {
    super.init()
        FirebaseApp.configure()
        // not really needed unless you really need it 
        FIRDatabase.database().persistenceEnabled = true
}

Happy Coding

1

iOS 9.2
Swift 2.1.1
Xcode 7.2.1
Mac OSX 10.10.5

Same error here using the following code:

AppDelegate.swift:

class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        // Override point for customization after application launch.

        FIRApp.configure()


        return true
    }

ViewController.swift:

import UIKit
import Firebase

class ViewController: UIViewController {


    var db = FIRDatabase.database().reference()

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        //Create some data in Firebase db:
        db.child("key").child("subkey").setValue("hello world")

    }

I also added the file GoogleService-Info.plist to my project directory as described in the Firebase Getting Started Guide.

And I made my Firebase db public with the following Rules:

{
  "rules": {
    ".read": true,
    ".write": true
  }
}

Making the following changes to ViewController.swift is what worked for me:

class ViewController: UIViewController {


    var db: FIRDatabaseReference!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        db = FIRDatabase.database().reference()
        db.child("key").child("subkey").setValue("hello world")

    }

Prior to running my app, my Firebase db looked like this:

myiosdata-abc123: null

After running my app, my Firebase db looked like this:

myiosdata-abc123
- key
   |
   +--- subkey: “hello world”
1

I had several normal working projects with FIRApp.configure () code in AppDelegate.swift:

class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        FIRApp.configure()
        return true
    }

Everything worked great for quite some time, but yesterday and today I opened a Swift 3 project inside my Xcode 7.3.1 (I am still working in Swift 2, but opened Swift 3 project to see what is changed), and suddenly in all my Swift 2 apps and projects that I am still working on, got the same error:

The default app has not been configured yet

Every project now when I open in XCode, getting same error, I didn't know what to do, but after implementing @MichaelWilliams' code, everything works fine again.

I have debug my Xcode (clear and reload console), but nothing works beside this new approach by Michael.

This one resolved my problem:

override init() {
   FIRApp.configure()
   FIRDatabase.database().persistenceEnabled = true
}

Can this new code somehow make my app's unstable and can I be afraid to see problems with connecting/working with Firebase database now?

1

Try re-download GoogleService-Info.plist from your console and add it to your project, That worked for me!

0

If you are using Xcode 9, Swift 4 and Firebase 4 please do the following:

override init() {
    FirebaseApp.configure()
    Database.database().isPersistenceEnabled = true
}
1
  • 1
    A good answer would include the file name (AppDelegate here) where we need to make changes.
    – CopsOnRoad
    Commented Sep 15, 2018 at 1:39
0

The cleanest solution to me here is to have lazy properties in case you want to have the db on top of your file. So let's say you have a FirebaseService class where you want to have Firestore.firestore() db constant to use it in all of the functions in that class:

private lazy var db = Firestore.firestore()

Or if you are using Firebase Storage:

private lazy var storage = Storage.storage().reference()

Also keep in mind that if you are referencing the Database/Storage in init() of your classes, you still might have the same problem so avoid that.

Not the answer you're looking for? Browse other questions tagged or ask your own question.