Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where do I add the database reference from Firebase on iOS?

I am following instructions for installation and setup of realtime database on iOS app from this link and I am confused where to add the statement self.ref = FIRDatabase.database().reference() in my app in xcode. Which file or location?

The photo is a screen shot of the link i wrote above

like image 841
Moeez Zahid Avatar asked Jun 27 '16 05:06

Moeez Zahid


People also ask

How do you reference Firebase?

You can reference the root or child location in your Database by calling firebase. database(). ref() or firebase. database().

How do I use Firebase with iOS app?

Go to the Firebase console. In the center of the project overview page, click the iOS+ icon to launch the setup workflow. If you've already added an app to your Firebase project, click Add app to display the platform options. Enter your app's bundle ID in the bundle ID field.

How do I create a reference in Firebase Realtime Database?

References are lightweight, so you can create as many as you need, and they are also reusable for multiple operations. To create a reference, get an instance of the Storage service using getStorage() then call ref() with the service as an argument. This reference points to the root of your Cloud Storage bucket.

Where can I find my Firebase reference URL in Firebase account?

Go to Database section. Tap Cloud Firebase (marked 1 in picture) and select Realtime Database. Marked 2 is the URL.


2 Answers

Firebase 4.0 and later: Make sure you add the following to Podfile! I had the same problem as you until I added this (found on the previous page of the Firebase setup tutorial).

pod 'Firebase/Database'

If you're like me, you ignored it because you thought you already added this line in the Firebase setup tutorial, but the line added in setup is:

pod 'Firebase/Core'

After you add that, it should work. A generic view controller would look like the following:

import Firebase
import FirebaseDatabase

class GenericViewController: UITableViewController {

    var ref: DatabaseReference!

    override func viewDidLoad() {
        super.viewDidLoad()

        ref = Database.database().reference()
    }
}

This had me stuck for a while - I hope this helps! :)

like image 160
Dominic Holmes Avatar answered Nov 15 '22 07:11

Dominic Holmes


Below is the code.

import UIKit
import Firebase
import FirebaseAuthUI


class ExampleVC: UIViewController, UINavigationControllerDelegate {

    var ref: FIRDatabaseReference!

    override func viewDidLoad() {
        super.viewDidLoad()

        //if user is signed in
        configureDatabase()
    }

    func configureDatabase() {
        //Gets a FIRDatabaseReference for the root of your Firebase Database.
        ref = FIRDatabase.database().reference()
    }

}
like image 20
Ashok R Avatar answered Nov 15 '22 08:11

Ashok R