Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to parse a firebase FDataSnapshot object?

my code retrieves structured data from firebase, but i was not clear how to get each value out of the firebase data object. i did not find the answer on stackoverflow, i am posting the question & answer here for other beginners.

my firebase snapshot object value:

{
    "08AD8779-6EEB-4449-BC77-78A661ADA72E" =     {
        field1 = "to device id";
        field2 = "text message";
    };
    "EB841471-618C-4C52-8AA0-C20AD2C947AC" =     {
        field1 = "to device id";
        field2 = "text message";
    };
} 

how to assign the device id (eg. "08AD8779-6EEB-4449-BC77-78A661ADA72E") and the value of 'field1' and 'field2' to NSString variables?

like image 903
tmr Avatar asked Aug 04 '14 04:08

tmr


1 Answers

following is the code that worked for me:

-(void)readFirebaseData {
    // Read data and react to changes
    [self.myRootRef observeEventType:FEventTypeValue withBlock:^(FDataSnapshot *snapshot) {

        for (FDataSnapshot* childSnap in snapshot.children) {
            NSString* otherDeviceName = childSnap.name;
            NSLog(@"otherDeviceName -> %@", childSnap.name);
            NSLog(@"otherDeviceField1 -> %@", childSnap.value[@"field1"]);
            NSLog(@"otherDeviceField2 -> %@", childSnap.value[@"field2"]);
        }     
    }];
}
like image 195
tmr Avatar answered Nov 04 '22 17:11

tmr