0

enter image description here

Driving me a bit crazy at this point. Trying to access imageName, yet always comes up blank. Help most appreciated.

 ref.observe(FIRDataEventType.value) {
        (snapshot: FIRDataSnapshot!) in
        if let result = snapshot.children.allObjects as? [FIRDataSnapshot] {

      for child in result {
        for child2 in child.children {

          // NO ERROR, and NO VALUE RETURNED
          let imageName = (child2 as? NSDictionary)?["imageName"] as? String ?? ""


          // NO VALUE RETURNED - LOOKING TO GRAB imageName HERE
          print("imageName: \(imageName)")

        }
      }
    }
  }
AL.
  • 36,815
  • 10
  • 142
  • 281
Edward Potter
  • 3,760
  • 4
  • 28
  • 39
  • Hi frank, thanks for the edits. This is a question that comes up LOTS. Answers are a mishmash of ways to do it. Old FB code, new FB code, etc. Would be AWESOME if the FB team just did a one pager on "best practices", would help out lots of people moving from Parse. – Edward Potter Oct 03 '16 at 22:41
  • http://stackoverflow.com/questions/28132620/can-i-manipulate-fdatasnapshot-once-i-have-queried-for-it looks like an interesting parse model. – Edward Potter Oct 04 '16 at 09:43

1 Answers1

1

In case you need to view each individual child and its values is better to use .childAdded instead.

let ref = FirDatabase.database().reference().child("12345")

ref.observe(.childAdded, with: { (snapshot) in

        guard let snap = snapshot.value as? NSDictionary else {
            return
        }

        let t = test()

        t.profileImageUrl = snap["imageName"] as? String
        t.name = snap["name"] as? String
        t.userID = snapshot.key

        print(snap["imageName"])

I used "t" supposing you have some kind of object that represents that node in the databse.

More info in the firebase docs, in retrieve data, child events

The FIRDataEventTypeChildAdded event is typically used to retrieve a list of items in a Firebase database. The FIRDataEventTypeChildAdded event is triggered once for each existing child and then again every time a new child is added to the specified path. The listener is passed a snapshot containing the new child's data.

  • https://www.firebase.com/docs/web/api/datasnapshot/ and this FB page are places to dive into. http://stackoverflow.com/questions/28132620/can-i-manipulate-fdatasnapshot-once-i-have-queried-for-it – Edward Potter Oct 04 '16 at 09:59
  • @EdwardPotter sorry I dont understand what you are trying to say.. Did you try what I told you? Btw that firebase page is from the old documents – Ricardo Alvarez Correa Oct 04 '16 at 16:00
  • Hi, what I've turned to is implementing swiftyJSON. Will report back. Like to get this down to to just a line or 2. Will check out new docs. I'll also try out your implementation today. thanks. Reference here: http://www.waltza.com/2015/12/10/firebase-and-swift-type-safety-with-swiftyjson/ – Edward Potter Oct 04 '16 at 17:00
  • There is this heated debate, should "t" be an Object or a Struct. Wow. – Edward Potter Oct 04 '16 at 18:04
  • @EdwardPotter well I just used an object to show you a way to store the properties. But here is a link to where they explained when is better to use classes or structs http://stackoverflow.com/questions/24232799/why-choose-struct-over-class/24232845 – Ricardo Alvarez Correa Oct 04 '16 at 18:11