按键值

时间:2016-10-11 10:34:42

标签: ios objective-c firebase firebase-realtime-database

我有这样的JSON。

   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;
        };
    }

我需要根据键category进行查询。

例如:如果我只想要提醒,它应该只检索等于"警报"的类别值。仅数据。

以下是检索livefeeds的查询。

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

1 个答案:

答案 0 :(得分:6)

首先直到孩子“livefeeds”然后“queryOrderedByChild”类别并像这样使用equal to孩子

[[[[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);
        }
    }
}];