Moeez Zahid
Moeez Zahid

Reputation: 76

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

Upvotes: 6

Views: 9638

Answers (4)

Dominic Holmes
Dominic Holmes

Reputation: 201

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! :)

Upvotes: 20

Ashok R
Ashok R

Reputation: 20776

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()
    }

}

Upvotes: 2

Hitesh Surani
Hitesh Surani

Reputation: 13557

Define variable in appDelegate file as follow(Global)

var ref: FIRDatabaseReference!

Add above statement in Appdelegate file(DidfinishlaunchingwithOption Method), so your database initialize when your app is launch.

self.ref = FIRDatabase.database().reference()
  • Add all other method or all statement related to fire base after this.

Upvotes: 1

JAB
JAB

Reputation: 3235

The instructions are unclear. In order for you to be able to assign value to 'ref' in AppDelegate, you must first declare an 'ref' property because one does not currently exist.

Put this in your AppDelegate with your property declarations:

var ref: FIRDatabaseReference!

Then place the line in the docs in the applicationDidFinishLaunching method and your code will compile. Whether you want to keep that property in AppDelegate is a different question.

Upvotes: 0

Related Questions