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?

- 565,676
- 79
- 828
- 807

- 76
- 1
- 3
4 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! :)

- 201
- 2
- 5
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()
}
}

- 19,892
- 8
- 68
- 68
-
1This no longer works for Firebase 4.0 released May 17 2017 – Famic Tech May 19 '17 at 19:15
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.

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

- 3,165
- 16
- 29