-1

How to read data from Firebase database? I have realtime firebase database with the following json file

{
  "Category" : {
    "Breakfast" : {
      "CategoryImg" : "Breakfast-img",
      "CategoryName" : "Breakfast"
    },
    "Gluten-Free" : {
      "CategoryImg" : "Gluten-free-img",
      "CategoryName" : "Gluten-free"
    },
    "Lunch" : {
      "CategoryImg" : "Lunch-img",
      "CategoryName" : "Lunch"
    },
    "Popular" : {
      "CategoryImg" : "Popular-img",
      "CategoryName" : "Popular"
    },
    "Trending" : {
      "CategoryImg" : "Trending-img",
      "CategoryName" : "Trending"
    },
    "Vegetarian" : {
      "CategoryImg" : "Vegetarian-img",
      "CategoryName" : "Vegetarian"
    }
  }
}

I'm using this code to retrieve the details

// 1
let rootRef = Database.database().reference()

// 2
let childRef = Database.database().reference(withPath: "Category")

// 3
let itemsRef = rootRef.child("Category")

// 4
let breakfastRef = itemsRef.child("Breakfast")

// 5
print(rootRef.key)
print(childRef.key)
print(itemsRef.key)
print(breakfastRef.key)

but I'm getting the following in the console. Can someone please help?

nil
Optional("Category")
Optional("Category")
Optional("Breakfast")

I'm following this tutorial but I'm unable to get the same result https://www.raywenderlich.com/3-firebase-tutorial-getting-started

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Tito
  • 29
  • 8
  • 2
    Your code is doing exactly what is expected. What are you expecting to print? – Galo Torres Sevilla Dec 15 '19 at 15:25
  • @GaloTorresSevilla I want to print category image and category name from firebase database – Tito Dec 15 '19 at 15:41
  • 1
    Then you shouldn't be printing the key. You should retrieve the data from firebase with `.observe` and print the snapshot. Or even better, cast it to a dictionary and print it. The tutorial actually shows how to do it. – Galo Torres Sevilla Dec 15 '19 at 15:57

1 Answers1

0

The tutorial you're following, says this in the code about what it will print:

// 5
print(rootRef.key)   // prints: ""
print(childRef.key)  // prints: "grocery-items"
print(itemsRef.key)  // prints: "grocery-items"
print(milkRef.key)   // prints: "milk"

It's impossible (or at least not feasible for me) to say why the results are different. It could be a change in SDK behavior, or in Swift behavior, but it's equally like that the article took some liberties in showing the output to focus on the Firebase aspects of it, instead of the Swift aspects.

But let's focus on the differences, and how you can deal with them in your applications.

The differences:

  • The first print(rootRef.key) prints nil for you, while it prints "" according to the tutorial.

    While the Firebase documentation for the Swift SDK doesn't say anything specific about the key of a root, the documentation for the Android SDK says this about DataSnapshot.getKey():

    The key name for the source location of this snapshot or null if this snapshot points to the database root.

    I quickly tested, and the JavaScript and Android SDKs indeed print null too.

    In other words: the behavior you're seeing is expected: since you're printing the key of the root of the database, the value is nil/null.

  • The other keys for you print as Optional(<value>) while the tutorial merely prints the <value>.

    This is expected behavior too. Since (as the first print statement shows) a key can either be nil or a value, its type is an Optional<String>. And when you print an optional string, Swift prints it as either nil (as in the first line) or as Optional(<value>).

    If you want to just print the value as in the tutorial, you'd use print(childRef.key!) (if you're certain the key is does not point to the root), or print(rootRef.key ?? "nil") otherwise.

    See How to remove Optional from String Value Swift for more on printing optionals.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • I'm trying to print the value of CategoryImg field. I have did the following `print(breakfastRef.value(forKey: "CategoryImg"))` but with no luck. the application crashes. I'm guessing that `print(childRef.key!)` is not retrieving the data from the database properly. – Tito Dec 15 '19 at 15:58