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.
我正在尝试将我的应用升级到新版本的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.'
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?
我在AppDelegate中有FIRApp.configure(),我将GoogleServices-Info.plist导入到我的项目中。 plist也有正确的信息。其他任何人遇到这个或知道如何解决它?
7 个解决方案
#1
52
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.
要配置Firebase,您必须在某处执行FIRApp.configure()。完成此操作后,您可以使用let firebaseDatabaseReference = FIRDatabase.database()。reference()来获取对该数据库的引用并开始使用它。问题不在于Firebase“本身”,而在于Swift的行为方式。
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.
如果你把FIRApp.configure()放在AppDelegate func应用程序中(应用程序:UIApplication,didFinishLaunchingWithOptions launchOptions:[NSObject:AnyObject]?) - > Bool然后在MyDatabase类中使用let firebaseDatabaseReference = FIRDatabase.database()。reference(在您声明的函数之外,有时代码FIRDatabase.database()。reference()在执行应用程序didFinishLaunchingWithOptions函数之前执行。
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."
基本上,您的类在有机会自行配置之前尝试获取对Firebase数据库的引用,在控制台中生成错误“尚未配置默认应用程序”。
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.
注意:这不会一直发生,有时应用程序启动很慢,例如在iOS模拟器中,并且在MyDatabase“let”执行并尝试获取引用之前没有机会完成。
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)
这就是为什么移动FIRApp.configure()代码以覆盖AppDelegate中的init()的原因,实际上它确保在初始化AppDelegate时执行配置代码(在此情况下,在大多数情况下,在MyDatabase初始化之前)
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.
还要确保你super.init()(所以你的超级类得到“消息”)所以你覆盖不是弊大于利。
#2
3
not really sure why conceptually but this worked...
不确定为什么在概念上但这有效...
In AppDelegate, outside of didFinishLaunchingWithOptions,
在AppDelegate中,在didFinishLaunchingWithOptions之外,
override init() {
FIRApp.configure()
FIRDatabase.database().persistenceEnabled = true
}
#3
2
iOS 9.2
Swift 2.1.1
Xcode 7.2.1
Mac OSX 10.10.5
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:
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:
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.
我还将文件GoogleService-Info.plist添加到我的项目目录中,如Firebase入门指南中所述。
And I made my Firebase db public with the following Rules:
我使用以下规则公开我的Firebase数据库:
{
"rules": {
".read": true,
".write": true
}
}
Making the following changes to ViewController.swift is what worked for me:
对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")
}
Prior to running my app, my Firebase db looked like this:
在运行我的应用程序之前,我的Firebase数据库看起来像这样:
myiosdata-abc123: null
After running my app, my Firebase db looked like this:
运行我的应用后,我的Firebase数据库看起来像这样:
myiosdata-abc123
- key
|
+--- subkey: “hello world”
#4
1
I had several normal working projects with FIRApp.configure ()
code in AppDelegate.swift
:
我在AppDelegate.swift中使用FIRApp.configure()代码进行了几个正常的工作项目:
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:
一切都运行了很长时间,但昨天和今天我在我的Xcode 7.3.1中打开了一个Swift 3项目(我仍然在Swift 2中工作,但是打开了Swift 3项目看看有什么变化),突然间我所有的我仍在使用的Swift 2应用程序和项目也出现了同样的错误:
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.
现在我在XCode中打开每个项目,得到同样的错误,我不知道该怎么做,但在实现了@MichaelWilliams的代码后,一切正常。
I have debug my Xcode (clear and reload console), but nothing works beside this new approach by Michael.
我已经调试了我的Xcode(清除和重新加载控制台),但迈克尔的这种新方法没有任何效果。
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?
这个新代码可以以某种方式使我的应用程序不稳定,我现在可以害怕看到连接/使用Firebase数据库的问题吗?
#5
0
Make sure you are having DATABASE_URL
key in your GoogleService-Info.plist
确保您的GoogleService-Info.plist中包含DATABASE_URL密钥
#6
0
If you are using Xcode 9, Swift 4 and Firebase 4 please do the following:
如果您使用的是Xcode 9,Swift 4和Firebase 4,请执行以下操作:
override init() {
FirebaseApp.configure()
Database.database().isPersistenceEnabled = true
}
#7
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:
对我来说最干净的解决方案是拥有延迟属性,以防你想在你的文件上放置数据库。因此,假设您有一个FirebaseService类,您希望Firestore.firestore()db常量可以在该类的所有函数中使用它:
private lazy var db = Firestore.firestore()
Or if you are using Firebase Storage:
或者,如果您使用的是Firebase存储:
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.
另外请记住,如果您在类的init()中引用数据库/存储,您仍可能遇到相同的问题,以避免这种情况。
#1
52
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.
要配置Firebase,您必须在某处执行FIRApp.configure()。完成此操作后,您可以使用let firebaseDatabaseReference = FIRDatabase.database()。reference()来获取对该数据库的引用并开始使用它。问题不在于Firebase“本身”,而在于Swift的行为方式。
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.
如果你把FIRApp.configure()放在AppDelegate func应用程序中(应用程序:UIApplication,didFinishLaunchingWithOptions launchOptions:[NSObject:AnyObject]?) - > Bool然后在MyDatabase类中使用let firebaseDatabaseReference = FIRDatabase.database()。reference(在您声明的函数之外,有时代码FIRDatabase.database()。reference()在执行应用程序didFinishLaunchingWithOptions函数之前执行。
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."
基本上,您的类在有机会自行配置之前尝试获取对Firebase数据库的引用,在控制台中生成错误“尚未配置默认应用程序”。
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.
注意:这不会一直发生,有时应用程序启动很慢,例如在iOS模拟器中,并且在MyDatabase“let”执行并尝试获取引用之前没有机会完成。
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)
这就是为什么移动FIRApp.configure()代码以覆盖AppDelegate中的init()的原因,实际上它确保在初始化AppDelegate时执行配置代码(在此情况下,在大多数情况下,在MyDatabase初始化之前)
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.
还要确保你super.init()(所以你的超级类得到“消息”)所以你覆盖不是弊大于利。
#2
3
not really sure why conceptually but this worked...
不确定为什么在概念上但这有效...
In AppDelegate, outside of didFinishLaunchingWithOptions,
在AppDelegate中,在didFinishLaunchingWithOptions之外,
override init() {
FIRApp.configure()
FIRDatabase.database().persistenceEnabled = true
}
#3
2
iOS 9.2
Swift 2.1.1
Xcode 7.2.1
Mac OSX 10.10.5
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:
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:
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.
我还将文件GoogleService-Info.plist添加到我的项目目录中,如Firebase入门指南中所述。
And I made my Firebase db public with the following Rules:
我使用以下规则公开我的Firebase数据库:
{
"rules": {
".read": true,
".write": true
}
}
Making the following changes to ViewController.swift is what worked for me:
对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")
}
Prior to running my app, my Firebase db looked like this:
在运行我的应用程序之前,我的Firebase数据库看起来像这样:
myiosdata-abc123: null
After running my app, my Firebase db looked like this:
运行我的应用后,我的Firebase数据库看起来像这样:
myiosdata-abc123
- key
|
+--- subkey: “hello world”
#4
1
I had several normal working projects with FIRApp.configure ()
code in AppDelegate.swift
:
我在AppDelegate.swift中使用FIRApp.configure()代码进行了几个正常的工作项目:
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:
一切都运行了很长时间,但昨天和今天我在我的Xcode 7.3.1中打开了一个Swift 3项目(我仍然在Swift 2中工作,但是打开了Swift 3项目看看有什么变化),突然间我所有的我仍在使用的Swift 2应用程序和项目也出现了同样的错误:
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.
现在我在XCode中打开每个项目,得到同样的错误,我不知道该怎么做,但在实现了@MichaelWilliams的代码后,一切正常。
I have debug my Xcode (clear and reload console), but nothing works beside this new approach by Michael.
我已经调试了我的Xcode(清除和重新加载控制台),但迈克尔的这种新方法没有任何效果。
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?
这个新代码可以以某种方式使我的应用程序不稳定,我现在可以害怕看到连接/使用Firebase数据库的问题吗?
#5
0
Make sure you are having DATABASE_URL
key in your GoogleService-Info.plist
确保您的GoogleService-Info.plist中包含DATABASE_URL密钥
#6
0
If you are using Xcode 9, Swift 4 and Firebase 4 please do the following:
如果您使用的是Xcode 9,Swift 4和Firebase 4,请执行以下操作:
override init() {
FirebaseApp.configure()
Database.database().isPersistenceEnabled = true
}
#7
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:
对我来说最干净的解决方案是拥有延迟属性,以防你想在你的文件上放置数据库。因此,假设您有一个FirebaseService类,您希望Firestore.firestore()db常量可以在该类的所有函数中使用它:
private lazy var db = Firestore.firestore()
Or if you are using Firebase Storage:
或者,如果您使用的是Firebase存储:
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.
另外请记住,如果您在类的init()中引用数据库/存储,您仍可能遇到相同的问题,以避免这种情况。