0

I need to get all the data from a specific table but their are times that the table does not exist in firebase. And I want to know if the reason is the table doesn't exist.

I'm using observe(.childAdded) btw.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
jay123456
  • 131
  • 1
  • 1
  • 10

2 Answers2

2

Try this if this will work

let ref = Database.database().reference()

ref.observeSingleEvent(of: .value) { (snapshot) in
    if snapshot.hasChild("mytable") {
        // exist
    } else {
        // does not exist
    }
}
elbert rivas
  • 1,464
  • 1
  • 17
  • 15
0

While Elbert's answer works, it downloads more data than needed. It downloads your entire database, to check if one node exists.

I recommend instead to only read the node that you're checking:

let ref = Database.database().reference()

ref.child("mytable").observeSingleEvent(of: .value) { (snapshot) in
    if snapshot.exists() {
        // exists
    } else {
        // does not exist
    }
}

Also see:

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807