Felix F. Felix F. - 1 year ago 120
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() {
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 Source

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

override func viewDidLoad() {
    self.ref = FIRDatabase.database().reference()
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download