Getting the key?

331 views
Skip to first unread message

Justin Bane

unread,
May 23, 2016, 7:52:55 PM5/23/16
to Firebase Google Group
How do you get the key from a retrieved set of data... because this doesn't work

for data in snapshot.children.nextObject {
   
print(data.key)
}


also... when is the documentation at least going to be as good as the legacy site? Need WAY MORE examples.

Jacob Wenger

unread,
May 23, 2016, 9:55:21 PM5/23/16
to fireba...@googlegroups.com
Hey Justin, which language are you using exactly? What version of the Firebase SDK are you using? Can you share some more surrounding code?

We are working to get the docs quality up. Where do you want to see more examples? In the guides? In the reference docs? All of the above?

--
You received this message because you are subscribed to the Google Groups "Firebase Google Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to firebase-tal...@googlegroups.com.
To post to this group, send email to fireba...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/firebase-talk/11e6ec55-6657-4812-a49b-172d245872d5%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Justin Bane

unread,
May 23, 2016, 10:29:40 PM5/23/16
to fireba...@googlegroups.com
Jacob, this is Swift. Unfortunately, I can't share much more... Should that work? The snapshot is a data set with keys (the auto generated type you get using child auto id)

I have a ton of places in my code using this type of code but now none of those work though I am getting data.

Also, I have to ask how the new system is being received? I find it quite a bit more difficult to use and it looks like I will have to do a top down rewrite of my app to use the new setup... Not cool.

As far as documentation, I would refer back to the legacy site - each sample code had basic examples of how to not only perform the query but how to access what you got back. The new guides have nothing close to that.

I did check the samples area but found they mostly impose a style on you. The don't give you concrete examples of how to use the most basic functionality. Something you need to show to developers looking to use your system... Basically, I think you need to lower the bar of entry.


You received this message because you are subscribed to a topic in the Google Groups "Firebase Google Group" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/firebase-talk/IMpyuKbP8P8/unsubscribe.
To unsubscribe from this group and all its topics, send an email to firebase-tal...@googlegroups.com.

To post to this group, send email to fireba...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.


--
---------------------------------------------------------------
Justin Bane
wk: 541-241-2263
cell: 916-712-2194
just...@gmail.com
---------------------------------------------------------------
www.justinbane.com

Katherine Fang

unread,
May 24, 2016, 12:53:57 AM5/24/16
to fireba...@googlegroups.com
Hi Justin!

Assuming you're using the newest SDK...

In Xcode 7.2, this seemed to work:

    for data in snapshot.children {
        print (data.key)
    }

In Xcode 7.3, that generated an "Ambiguous use of 'key'" error, but this worked:

    for data in snapshot.children {
        let d = data as! FIRDataSnapshot
        print(d.key)
    }

Hope that helps, but if those don't work, can you share what version of Xcode and Firebase you're using? 

- Kat

Justin Bane

unread,
May 24, 2016, 11:02:59 AM5/24/16
to Firebase Google Group
Thank you, Katherine

I am using the latest of everything.

Your solution worked! Only lost an entire day trying to figure it out. These are the kind of things that need documented. I would have never guessed at what needed to happen and there is nowhere in the docs to help with this.

I would imagine that TONS of people have code written like mine if they followed the old documentation as I did. The old docs were great at getting you successfully pulling/pushing data.

The new docs NEED to identify the differences between the old and the new system. For those of us that have a ton of code to rewrite.

---------------------------------------------------------------
Justin Bane
wk: 541-241-2263
cell: 916-712-2194
just...@gmail.com
---------------------------------------------------------------
www.justinbane.com

Justin Bane

unread,
May 24, 2016, 11:08:28 AM5/24/16
to Firebase Google Group
OK, so here's the next question.

in the same loop I now cannot get the data as it was being retrieved.

for data in snapshot.children {

                let d = data as! FIRDataSnapshot

                print(d["actual-cell-type"])

}

This fails on build saying that 'Type FIRDataSnapshot' has no subscript members.


---------------------------------------------------------------
Justin Bane
wk: 541-241-2263
cell: 916-712-2194
just...@gmail.com
---------------------------------------------------------------
www.justinbane.com

Justin Bane

unread,
May 24, 2016, 11:11:03 AM5/24/16
to Firebase Google Group
Also the data in "d" looks like this.

Snap (-JuNdhoQQFzPCa2g_s-R) {

    active = 1;

    "actual-cell-type" = Teratocarcinoma;

    "actual-species" = Human;

    "claimed-cell-type" = "Ovarian carcinoma";

    "claimed-species" = Human;

    "contaminating-cell-line" = PA1;

    "misidentified-cell-line" = 222;

    "reference-pubmed-id" = 22710073;

    "reported-by" = "Korch et al, 2012";

}

Katherine Fang

unread,
May 24, 2016, 11:35:54 AM5/24/16
to Firebase Google Group
Hi Justin,

`d` is still a FIRDataSnapshot, so it's treated the same as `snapshot`. (Here are the reference docs for FIRDataSnaphshot: https://firebase.google.com/docs/reference/ios/firebasedatabase/interface_f_i_r_data_snapshot)

You'll find the value under `d.value`, so here's two different ways you can grab the data.

    for data in snapshot.children {
        guard let datasnapshot = data as? FIRDataSnapshot else { continue }
        // Get the value rather unsafely, but this is maybe okay if you have the right validation in your security rules
        print(datasnapshot.value!["subkey"])

        // Make sure the value is actually a dictionary before trying to dip into it.
        guard let datavalue = datasnapshot.value as? [String:AnyObject] else { continue }
        print(datavalue["sub"])
    }

Someone let me near new syntax. Excuse my excitement. :)

- Kat

--
You received this message because you are subscribed to the Google Groups "Firebase Google Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to firebase-tal...@googlegroups.com.
To post to this group, send email to fireba...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages