Felix F. Felix F. - 3 months ago 40
iOS Question

FIRDatabaseReference returns nil, UserID retrievable though

I'm trying to retrieve data from my Firebase database, but I'm stuck at the database reference returning nil. However, I successfully can retrieve the UserID, so I don't think it's not properly connected to my database:

import UIKit
import Firebase
import FirebaseDatabaseUI

class LoggedInViewController: UIViewController {

var ref: FIRDatabaseReference!
let userid = FIRAuth.auth()?.currentUser?.uid

override func viewDidLoad() {
super.viewDidLoad()
UIDLabel.text = String((userid)!) // The Label shows the UID
reflabel.text = String(ref) // The Label shows "nil"
}
}


My goal is to retrieve data from the path using this method:

func getQuery() -> FIRDatabaseQuery {
let myTopPostsQuery = (ref.child("user-posts")).child(getUid()))
return myTopPostsQuery
}


But obviously this doesn't work since I'm getting an error because of the nil reference.

Answer

In viewDidLoad use FIRDatabase.database().reference()

override func viewDidLoad() {
    super.viewDidLoad()
    self.ref = FIRDatabase.database().reference()
    //....
}