【问题标题】:Retrieve data from new Firebase从新的 Firebase 检索数据
【发布时间】:2016-09-15 22:02:01
【问题描述】:

请帮忙。迁移到新的 Firebase 后,我无法检索数据。 使用这个结构:

let ref = FIRDatabase.database().reference()

override func viewDidLoad() {
    super.viewDidLoad()
    ref.observeEventType(FIRDataEventType.Value, withBlock: { (snapshot) in
        let postDict = snapshot.value as! [String : AnyObject]
        print("\(postDict)")
    })
}

运行后我看到错误:

2016-05-19 10:04:22.264 123a[88652:13688922] The default app has not been configured yet.
2016-05-19 10:04:22.276 123a[88652:13688922] *** Terminating app due to uncaught exception 'MissingDatabaseURL', reason: 'Failed to get FIRDatabase instance: FIRApp object has no databaseURL in its FirebaseOptions object.'
*** First throw call stack:

我阅读了文档,但无法解决此问题。 GoogleService-Info.plist 我添加到项目中。

【问题讨论】:

  • 你之前用过 FIRApp.configure() 吗?
  • @GabrieleMariotti 是的,当然

标签: xcode swift firebase


【解决方案1】:

我还没有看到这个答案,我必须将配置调用添加到 AppDelegate init 方法。所以它看起来像:

override init() {
    super.init()
    // Firebase Init
    FIRApp.configure()
}

【讨论】:

  • 谢谢!谷歌文档仅涵盖我不喜欢的 pod build。
【解决方案2】:

遇到了同样的问题。 我寻找与 plist 相关的链接问题,但这不是问题。 我想可能是因为我的初始视图控制器在配置完成之前被撤销了。 我通过实验解决了这个问题。

我最初的视图控制器是这样的:

    let ref = FIRDatabase.database().reference()

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

}

我已将其更改为:

    var ref = FIRDatabaseReference.init()

override func viewDidLoad() {
    super.viewDidLoad()
    ref = FIRDatabase.database().reference()

    // Do any additional setup after loading the view, typically from a nib.

}

崩溃已解决。

【讨论】:

  • 此方法解决崩溃,但我不从 firebase 获取数据
  • 这些解决方案都不适合我。我尝试了其中的每一个。并不断收到同样的错误。
  • 同样的问题,克里斯。你解决了吗?
  • 我也遇到了同样的问题,我都试过了,还有什么要补充的吗?
【解决方案3】:

所以,在我的视图控制器实例化时,我也立即声明了一个 ref。在应用程序委托中使用FIRApp.configure() 配置应用程序后,我必须加载它。

之前:

let serverRef = Firebase("firebaseURL")

之后:

lazy var serverRef = FIRDatabase.database().reference()

这会延迟数据库引用的实例化,直到需要它为止,直到您的初始视图控制器上的viewDidLoad

【讨论】:

    【解决方案4】:

    以@ColdLogic 给出的答案为基础,出现此错误的原因是我的 Firebase 数据库引用是在视图控制器的 init 方法中创建的,不是在 viewDidLoad 方法中.由于应用程序启动时实例化的所有类的 init 方法在 AppDelegate 中的 application:DidFinishLaunchingWithOptions 方法之前调用 ,因此导致了此崩溃。移动这行代码:

    class MyViewController {
    
       var firebaseRef: FIRDatabaseReference
    
       required init?(coder aDecoder: NSCoder) {
          ...
          firebaseRef = FIRDatabase.database().reference()
       }
    
       override func viewDidLoad() {
          ...
       }
    }
    

    到这里:

    class MyViewController {
    
       var firebaseRef: FIRDatabaseReference
    
       required init?(coder aDecoder: NSCoder) {
          ...
       }
    
       override func viewDidLoad() {
          ...
          self.firebaseRef = FIRDatabase.database().reference()
       }
    }
    

    为我解决了问题。

    【讨论】:

      【解决方案5】:

      我的 Firebase 数据库也有问题。通过添加修复它

      import FirebaseDatabase
      

      到我的代码

      【讨论】:

        【解决方案6】:

        今天遇到了同样的问题,您需要在 google-services.json 中输入 "firebase_url": "https://xxxxxxxxxx.firebaseio.com" 并为此执行此步骤 https://support.google.com/firebase/answer/7015592#android 如果你之前有一个来自谷歌云平台的文件,可能会有一些差异,你必须检查一下。对我来说这是可行的。

        【讨论】:

        • 最好将链接中的相关部分包含在答案中。链接可能会消失。
        • 我在 google plist 文件中设置了键“firebase_url”,但这对我没有帮助
        【解决方案7】:

        在我的情况下,我必须在调用超级 applicationDidLaunch 之前更改要调用的配置:

        [FIRApp configure];
        
        [super application:application didFinishLaunchingWithOptions:launchOptions];
        

        【讨论】:

        • 我使用 Swift。在我的项目中,在 AppDelegate 中的 application:application didFinishLaunchingWithOptions:launchOptions 方法之后设置 FIRApp.configure()。但这对我没有帮助
        【解决方案8】:

        在我将FIRApp.configure() 设为AppDelegate didFinishLaunchingWithOptions 的第一行之前,我遇到了这个错误

        【讨论】:

          【解决方案9】:

          确保您已从 Firebase 控制台下载 GoogleService-Info.plist 文件并添加到项目目录的根目录中。

          添加后,从 AppDelegate 中的 didFinishLaunchingWithOptions 调用此函数:

          FIRApp.configure()
          

          就是这样,它应该已经启动并运行了!

          【讨论】:

          • @jankigadhiya 我似乎在这里找不到新问题,答案看起来不错
          • @jankigadhiya 这不应该被否决。感谢 egor.zhdan 的回复。
          • @JunaidMukhtar 我没有投反对票,对评论感到抱歉
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-01-15
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多