【问题标题】:The default app has not been configured yet默认应用尚未配置
【发布时间】:2016-09-16 23:44:15
【问题描述】:

我正在尝试将我的应用升级到新版本的 Firebase。我浏览了设置指南并编辑了所有代码以匹配新语法。但是,当我运行该应用程序时,出现这两个错误。

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

我在 AppDelegate 中有 FIRApp.configure(),我将 GoogleServices-Info.plist 导入到我的项目中。 plist 也包含所有正确的信息。还有其他人遇到这个问题或知道如何解决吗?

【问题讨论】:

  • 您是否尝试过查看these 的一些答案?

标签: ios swift firebase


【解决方案1】:

AppDelegate.m 中,在didFinishLaunchingWithOptions 之外,

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

【讨论】:

  • 你一直有persistenceEnabled调用吗?首先被调用将触发 FIRDatabase 类来检查它是否已配置并失败。只要将persistenceEnabled标志与它一起移动,就可以将配置移回原来的位置。
【解决方案2】:

iOS 9.2
斯威夫特 2.1.1
Xcode 7.2.1
Mac OSX 10.10.5

同样的错误在这里使用以下代码:

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

    }

我还将文件GoogleService-Info.plist 添加到我的项目目录中,如Firebase Getting Started Guide 中所述。

我使用以下 Rules 公开了我的 Firebase 数据库:

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

对 ViewController.swift 进行以下更改对我有用:

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

    }

在运行我的应用之前,我的 Firebase 数据库如下所示:

myiosdata-abc123: null

运行我的应用后,我的 Firebase 数据库如下所示:

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

【讨论】:

    【解决方案3】:

    我在AppDelegate.swift 中有几个使用FIRApp.configure () 代码的正常工作项目:

    class AppDelegate: UIResponder, UIApplicationDelegate {
    
        var window: UIWindow?
    
    
        func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
            FIRApp.configure()
            return true
        }
    

    在相当长的一段时间内一切都很好,但是昨天和今天我在Xcode 7.3.1 中打开了一个Swift 3 项目(我仍在Swift 2 工作,但打开了Swift 3 项目以查看发生了什么变化),突然,在我仍在处理的所有Swift 2 应用程序和项目中,出现了同样的错误:

    尚未配置默认应用

    现在,当我在 XCode 中打开每个项目时,都会遇到同样的错误,我不知道该怎么办,但是在执行 @MichaelWilliams 的代码后,一切都恢复正常了。

    我已经调试了我的 Xcode(清除并重新加载控制台),但除了 Michael 的这种新方法之外,没有任何效果。

    这个解决了我的问题:

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

    这个新代码会以某种方式使我的应用变得不稳定吗?我现在是否害怕看到连接/使用 Firebase 数据库时出现问题?

    【讨论】:

      【解决方案4】:

      这是您问题的答案:

      要配置 Firebase,您必须在某处执行 FIRApp.configure()。完成此操作后,您可以使用 let firebaseDatabaseReference = FIRDatabase.database().reference() 获取对该数据库的引用并开始使用它。问题不在于 Firebase “本身”,而在于 Swift 的行为方式。

      如果您将 FIRApp.configure() 放入您的 AppDelegate func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool,然后在 MyDatabase class 中使用 let firebaseDatabaseReference = FIRDatabase.database().reference() 在您声明的函数之外有时代码 @ 987654325@ 在application didFinishLaunchingWithOptions 函数执行之前执行。

      本质上,您的班级正在尝试获取对 Firebase 数据库的引用它有机会配置自己,在控制台中生成错误“尚未配置默认应用程序。”

      注意:这种情况并非一直发生,有时应用程序启动缓慢,例如在 iOS 模拟器中,并且在 MyDatabase "let" 执行并尝试执行之前它没有机会完成获得参考。

      这就是为什么移动 FIRApp.configure() 代码以覆盖 AppDelegate 中的 init() 的原因,本质上它确保在 AppDelegate 初始化时执行配置代码(在这种情况下和大多数情况下,在 MyDatabase 初始化之前)

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

      还要确保您使用 super.init()(这样您的超类会收到“消息”),这样您的覆盖不会弊大于利。

      【讨论】:

      • 我通过将我的ref = FIRDatabase.database().reference() 移动到我的 UIViewController 的viewDidLoad 方法中解决了这个问题,并声明了var ref : FIRDatabaseReference!。这有什么缺点,还是有理由更喜欢覆盖AppDelegateinit()
      • 您不必将其移至 init() 函数,只需确保在配置之前没有任何其他可全局访问的结构/类等访问 Firebase 对象。
      • 你的解释让我明白了,感谢它对我的完美。
      • 我有全局变量访问 Firebase 对象,即let db = Firestore.firestore(),因为我可以保证它不是零。如果我等到 onViewDidLoad 将其设置为相等,那么我必须将它们变成可选项,我希望尽可能避免。
      • 我不知道为什么,但是这样做会禁用应用程序委托调动。因此,如果您使用的是依赖于应用程序委托调动的电话身份验证,那么您可能不想将配置函数放在 init 中。
      【解决方案5】:

      确保您的GoogleService-Info.plist 中有DATABASE_URL 密钥

      【讨论】:

      • 这对我来说是个问题。再次下载它并且这个键值对存在。现在出现了一个新问题,SDK 似乎不知道如何处理美国以外的数据库位置。我使用了比利时,SDK 无法解析 URL。我用的是美国,它奏效了。
      【解决方案6】:

      如果您使用的是 Xcode 9、Swift 4 和 Firebase 4,请执行以下操作:

      override init() {
          FirebaseApp.configure()
          Database.database().isPersistenceEnabled = true
      }
      

      【讨论】:

      • 一个好的答案应该包括我们需要进行更改的文件名(此处为AppDelegate)。
      【解决方案7】:

      对我来说最干净的解决方案是拥有 惰性属性,以防您希望将数据库放在文件顶部。因此,假设您有一个 FirebaseService 类,您希望在其中拥有 Firestore.firestore() db 常量,以便在该类的所有函数中使用它:

      private lazy var db = Firestore.firestore()
      

      或者,如果您使用的是 Firebase 存储:

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

      另外请记住,如果您在类的 init() 中引用数据库/存储,您仍然可能遇到同样的问题,因此请避免。

      【讨论】:

        【解决方案8】:

        我也在使用Fabric,在我的情况下,它是FabricFirebase 初始化的顺序。我必须先初始化Firebase

        变化很大

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

        到:

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

        解决了问题。

        【讨论】:

          【解决方案9】:

          Swift 5 - 简单的解决方案

          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
          }
          

          快乐编码

          【讨论】:

            【解决方案10】:

            尝试从您的控制台重新下载 GoogleService-Info.plist 并将其添加到您的项目中,这对我有用!

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 2018-12-01
              • 2020-04-02
              • 1970-01-01
              • 2023-02-09
              • 2021-09-22
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多