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?
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"]);
}
}];
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With