【问题标题】:IOS Firebase query access by valueIOS Firebase 按值查询访问
【发布时间】:2016-09-23 13:18:10
【问题描述】:

如何使用查询按值获取给定的 json 对象。详细来说,想得到1 for id:4 不循环json表中的所有事件

【问题讨论】:

  • 您使用的是哪种语言? Obj-C 还是 Swift?你的标签有点混乱......

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


【解决方案1】:

斯威夫特 2

FIRDatabase.database().reference().child("events").queryOrderedByChild("id").queryEqualToValue(4).observeSingleEventOfType(.Value, withBlock: {(Snap) in 
  if let snapDict = Snap.value as? [String:AnyObject]{
      for each in snapDict{
          print(each.0) // Will print out your node ID = 1
          print(each.1) //Will print out your Dictionary inside that node.
          }

      }

  })

斯威夫特 3

 FIRDatabase.database().reference().child("events").queryOrdered(byChild: "id").queryEqual(toValue: 4).observeSingleEvent(of: .value, with: {(Snap) in
        if let snapDict = Snap.value as? [String:AnyObject]{
            for each in snapDict{
                print(each.0) // Will print out your node ID = 1
                print(each.1) //Will print out your Dictionary inside that node.
            }

        }

    })

【讨论】:

  • 如果它解决了您的问题,请接受它作为答案:) 快乐编码
  • 多么安静的代码。它真的帮助了我。非常感谢
【解决方案2】:

或许能解决你的问题

[self.dbRef observeEventType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot * _Nonnull snapshot) {
    if (![snapshot.value isKindOfClass:[NSNull class]]) {
        NSArray *arrResponse = (NSArray *)snapshot.value;

        NSPredicate *pred = [NSPredicate predicateWithFormat:@"id = %@", serchID];
        NSArray *arrSearchedVal = [arrResponse filteredArrayUsingPredicate:pred];
    }
}];

注意: dbRef 是您的 FIRDatabaseReference,它返回整个数据 JSON。然后将 NSPredicate 与您的搜索 id 一起使用,您将获得结果。

编码愉快..

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-03-21
    • 2017-05-11
    • 2019-07-21
    • 2018-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-06
    相关资源
    最近更新 更多