Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

iosmike

macrumors member
Original poster
Apr 28, 2016
32
5
Toronto, Canada
I am trying to retrieve every object stored in Firebase. Not sure how to go ahead. The following is my code and snap.value printed. I need to retrieve String "cbc" and "cp24" as well as values for datesaved and url. "cbc" and "cp24" are names I entered when saving.

My code:

Code:
Ref_UsersBase.observeEventType(.Value, withBlock:{ snapshot in

if let snapshots = snapshot.children.allObjects as?[FDataSnapshot]{

            for snap in snapshots {

                print(snap.value)


            }           
        }
    })

{

// Here is snap.value printed:

{
"-KGOzXm1lLtyS23cOR-G"=     {
    cp24 =         {
        datesaved ="Apr 27, 2016";
        url ="https://www.google.ca/search?site=&source=hp&ei=H0chV6pfhKeOBJChpqAN&q=cp24+weather&oq=&gs_l=mobile-gws-hp.1.0.41l2.0.0.0.13343.1.1.0.1.1.0.368.368.3-1.1.0....0...1..64.mobile-gws-hp..0.1.6.2.JC6OI95nuYc";
    };
};
"-KGOznhRN0J2OVlIogZ0"=     {
    cbc =         {
        datesaved ="Apr 27, 2016";
        url ="http://www.cbc.ca/beta/news/canada/toronto";
    };
};
}
 
Last edited by a moderator:
Not sure I understand the Firebase schema. Shouldn't the structure all be the same? Your key would be the generated "-KG..." and each one would contain the same JSON key/value pairs. Here you have cp24 and cbc which hold an array. Probably just me but the structure is confusing me looking at it.

Using .Value gets everything at once. Using .ChildAdded lets you take each child one at a time as well as grab new additions.

How I retrieve and parse Firebase data is (off the top of my head):

create my reference url at the correct level
add an observer
parse the key/value data as it comes in.
Code:
var ref = Firebase(url: "https://......")
ref.observeEventType(.ChildAdded, withBlock: { snapshot in

// full debug view
print(snapshot.key)
print(snapshot.value)
// maybe grab each value as an array and then grab the key/value from it...

// or grab something...
// if let theURL = snapshot.value["url"] as? String {
//  print(theURL) }

})

Basically, you're getting JSON and need to parse the key/value pairs at your child which you should know the keys and what type the value is (string, bool, int, etc).

You can create or move your ref down farther in the tree to make things easier if you know the path
 
Not sure I understand the Firebase schema. Shouldn't the structure all be the same? Your key would be the generated "-KG..." and each one would contain the same JSON key/value pairs. Here you have cp24 and cbc which hold an array. Probably just me but the structure is confusing me looking at it.

Using .Value gets everything at once. Using .ChildAdded lets you take each child one at a time as well as grab new additions.

How I retrieve and parse Firebase data is (off the top of my head):

create my reference url at the correct level
add an observer
parse the key/value data as it comes in.
Code:
var ref = Firebase(url: "https://......")
ref.observeEventType(.ChildAdded, withBlock: { snapshot in

// full debug view
print(snapshot.key)
print(snapshot.value)
// maybe grab each value as an array and then grab the key/value from it...

// or grab something...
// if let theURL = snapshot.value["url"] as? String {
//  print(theURL) }

})

Basically, you're getting JSON and need to parse the key/value pairs at your child which you should know the keys and what type the value is (string, bool, int, etc).

You can create or move your ref down farther in the tree to make things easier if you know the path
Thank you and I will try. These are very helpful clues.
 
This is how I do it. Let's say I have a Firebase structure like this:

Code:
EPL
|--teams
      |--name
      |--shortName
      |--crestUrl

Such that under "teams" there are 20 records all with name, shortName, crestUrl strings (and others I might ignore)

If I want to load this data I just create a reference at the level I want and parse each one as it comes in.

Code:
Firebase(url: "https://xxxxxxx.firebaseio.com/teams").observeEventType(.ChildAdded, withBlock: { snapshot in
            if let title = snapshot.value.objectForKey("name") as? String {
                print(title)
            }
            if let shorty = snapshot.value.objectForKey("shortName") as? String {
                print(shorty)
            }
            if let crest = snapshot.value.objectForKey("crestUrl") as? String {
                print(crest)
            }
        })

As each record is retrieved, I can do whatever I want. Load a table, insert into an array, do any logical statements based on results, etc.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.