Anna F
Anna F

Reputation: 1683

How to configure Firebase for Swift and read data out

I have installed Firebase for my Swift IOS App

In my AppDelegate file I import Firebase like this:

  import Firebase
    import FirebaseDatabase

and initialise it:

FirebaseApp.configure()

After that I check if database is connected and it prints out first "Not connected", but then "Connected", so I assume it is successful

    var ref:DatabaseReference!
    var databaseHandle:DatabaseHandle!

    override func viewDidLoad() {
     let connectedRef = Database.database().reference(withPath: ".info/connected")
        connectedRef.observe(.value, with: { snapshot in
            if snapshot.value as? Bool ?? false {
                print("Connected")
            } else {
                print("Not connected")
            }
        })
}

But then I try to access my data and it gives me nothing!

 ref = Database.database().reference()
    databaseHandle = ref.child("Meal").observe(.childAdded, with: { (snapshot) in
        print(snapshot.value as Any)

Here is snapshot from console Firebase enter image description here

Also i can switch to realtime database, where is no data, but there i can allow reading/writing access (I set both to true)

  "rules": {
    ".read": true,
    ".write": true
  }

What could be the problem, so I can't get my data printed out?.... enter image description here

Upvotes: 2

Views: 749

Answers (2)

Yogesh Tandel
Yogesh Tandel

Reputation: 1754

There is a difference in accessing Firebase Realtime Database and firestore data

You can access your cloud firestore data as shown below

In ViewDidLoad

let db = Firestore.firestore()
let ref =  db.collection("Meal").document("\(myRiderUID!)")
db.collection("Meal").getDocuments() { (querySnapshot, err) in
    if let err = err {
        print("Error getting documents: \(err)")
    } else {
        for document in querySnapshot!.documents {
            print("\(document.documentID) => \(document.data())")
        }
    }
}

Upvotes: 0

Do2
Do2

Reputation: 1791

The problem is that you are trying to access Firestore with Firebase methods.

"But then I try to access my data and it gives me nothing!" It gives you nothing because there is no data in Firebase, all your data is in Firestore.

See this guide for Firestore: https://firebase.google.com/docs/firestore/quickstart

and this one to understand the differences between Firebase and Firestore: https://firebase.google.com/docs/database/rtdb-vs-firestore

Upvotes: 2

Related Questions