【问题标题】:Firebase database query by key valueFirebase 数据库按键值查询
【发布时间】:2017-03-27 13:55:41
【问题描述】:

我有这样的 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);
  }];

【问题讨论】:

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


    【解决方案1】:

    首先到子“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);
            }
        }
    }];
    

    【讨论】:

    • 哦,谢谢。我不知道我是怎么错过的。当我尝试它时,我得到了“空”的结果。但现在它的工作,
    猜你喜欢
    • 2017-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-05
    • 2018-06-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多