Retrieve data from new Firebase

2020-06-18 00:54发布

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.

9条回答
淡お忘
2楼-- · 2020-06-18 01:34

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.

查看更多
劳资没心,怎么记你
3楼-- · 2020-06-18 01:39

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()
}
查看更多
淡お忘
4楼-- · 2020-06-18 01:49

I too had a problem with the Firebase Database. Fixed it by adding

import FirebaseDatabase

to my code

查看更多
甜甜的少女心
5楼-- · 2020-06-18 01:50

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.

查看更多
狗以群分
6楼-- · 2020-06-18 01:52

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.

查看更多
家丑人穷心不美
7楼-- · 2020-06-18 01:52

In my case I had to change the configure to be called before calling the super applicationDidLaunch:

[FIRApp configure];

[super application:application didFinishLaunchingWithOptions:launchOptions];
查看更多
登录 后发表回答