可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
Please help. After migrating to new Firebase I can't retrieve data.
Use this construction:
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)")
})
}
After running I see error:
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:
I read documentation, but can't resolve this problem.
GoogleService-Info.plist I add to project.
回答1:
I didn't see this answer yet, I had to add the configure call to the AppDelegate init method. So it looks like:
override init() {
super.init()
// Firebase Init
FIRApp.configure()
}
回答2:
Had the same problem.
I looked for linking problems that are related to the plist but that wasn't the problem.
I thought maybe it has caused because of that my initial view controller is revoked before the configurations are completed.
I solved the problem by experimenting a bit.
My initial view controller was this:
let ref = FIRDatabase.database().reference()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
I've changed that to this:
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.
}
Crash resolved.
回答3:
So, with mine, I also had a ref being declared immediately when the view controller was instantiated. I had to make it load after the app had been configured in the app delegate with FIRApp.configure()
.
Before:
let serverRef = Firebase("firebaseURL")
After:
lazy var serverRef = FIRDatabase.database().reference()
This delays the instantiation of the database reference until its needed, which wont be until viewDidLoad
on your initial view controller.
回答4:
To build on the answer given by @ColdLogic, the reason I had this error was because I had my Firebase database reference being created in an init method on a view controller, not in the viewDidLoad method. Since the init methods for all classes that are instantiated when the app launches are called before the application:DidFinishLaunchingWithOptions method in the AppDelegate, it was causing this crash. Moving this line of code:
class MyViewController {
var firebaseRef: FIRDatabaseReference
required init?(coder aDecoder: NSCoder) {
...
firebaseRef = FIRDatabase.database().reference()
}
override func viewDidLoad() {
...
}
}
to here:
class MyViewController {
var firebaseRef: FIRDatabaseReference
required init?(coder aDecoder: NSCoder) {
...
}
override func viewDidLoad() {
...
self.firebaseRef = FIRDatabase.database().reference()
}
}
solved the problem for me.
回答5:
I too had a problem with the Firebase Database. Fixed it by adding
import FirebaseDatabase
to my code
回答6:
Had the same problem today, you need the "firebase_url": "https://xxxxxxxxxx.firebaseio.com" at google-services.json and for that do this steps https://support.google.com/firebase/answer/7015592#android
If you had one file from google cloud platform before, maybe there are some differences and you have to check. For me this works.
回答7:
In my case I had to change the configure to be called before calling the super applicationDidLaunch:
[FIRApp configure];
[super application:application didFinishLaunchingWithOptions:launchOptions];
回答8:
I was getting this error until I made FIRApp.configure()
the first line in the AppDelegate
didFinishLaunchingWithOptions
回答9:
Make sure you have downloaded the GoogleService-Info.plist
file from your Firebase console and added to the root of your project directory.
Once you have added it call this function from didFinishLaunchingWithOptions
in AppDelegate:
FIRApp.configure()
Thats it, it should be up and running!