6

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

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Moeez Zahid
  • 76
  • 1
  • 3

4 Answers4

20

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

Dominic Holmes
  • 201
  • 2
  • 5
2

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

}
Ashok R
  • 19,892
  • 8
  • 68
  • 68
1

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.
Hitesh Surani
  • 12,733
  • 6
  • 54
  • 65
  • 2
    this doesn't answer the question - there is no ref variable in AppDelegate, so this would not compile. – JAB Jul 30 '16 at 05:38
0

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.

JAB
  • 3,165
  • 16
  • 29