Cade Cade - 1 month ago 32
Objective-C Question

Firebase database query by key value

I have JSON like this.

livefeeds= {
"-KTn8pbqFHBUvgJ1Gwyl" = {
category = Alerts;
text = "Samsung Galaxy Note 7 owners told to turn off device";
timestamp = 1476179485648;
"user_id" = V7EFBV6uATf8urLXX9eK4EHhxmG2;
};
"-KTn8pbrhHyNzeLh2vOq" = {
category = News;
text = "Chicago Teachers Union, school board reach tentative contract agreement";
timestamp = 1476179485648;
"user_id" = V7EFBV6uATf8urLXX9eK4EHhxmG2;
};
}


I need to make query based on the key
category
.

Ex: If I only want Alerts, it should only retrieve category value equal to "Alerts" data only.

Here is query to retrieve
livefeeds
.

[[self.ref queryOrderedByChild:@"livefeeds"] observeEventType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot * _Nonnull snapshot) {
NSLog(@"---> %@",snapshot.value);
}];

Answer

First go till the "livefeeds" and than "queryOrderedByChild" category and use equal to child like this way

[[[[self.ref child:@"livefeeds"] queryOrderedByChild:@"category"]   
                                   queryEqualToValue:groupId]   
                                    observeEventType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot * _Nonnull snapshot) {

    if (snapshot.value != [NSNull null]){
        for (NSDictionary *snap in [snapshot.value allValues]) {
             NSLog(@"---> %@",snap);
        }
    }
}];
Comments