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:53

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.

查看更多
混吃等死
3楼-- · 2020-06-18 01:53

I was getting this error until I made FIRApp.configure() the first line in the AppDelegate didFinishLaunchingWithOptions

查看更多
小情绪 Triste *
4楼-- · 2020-06-18 01:59

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!

查看更多
登录 后发表回答