The default app has not been configured yet

后端 未结 9 1537
予麋鹿
予麋鹿 2020-12-02 19:59

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,

相关标签:
9条回答
  • 2020-12-02 20:45

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

    0 讨论(0)
  • 2020-12-02 20:46

    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.

    0 讨论(0)
  • 2020-12-02 20:48

    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 讨论(0)
  • 2020-12-02 20:51

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

    override init() {
        FirebaseApp.configure()
        Database.database().isPersistenceEnabled = true
    }
    
    0 讨论(0)
  • 2020-12-02 20:53

    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.

    0 讨论(0)
  • 2020-12-02 20:54

    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?

    0 讨论(0)
提交回复
热议问题