通过深层次的孩子的价值查询数据 [英] Query data by value of deep child

查看:149
本文介绍了通过深层次的孩子的价值查询数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  groups 
-KOrPKM2QUzuMnMlHfJu
name:Testgroup 1
成员
0:123456789
1:987654321

-KOrPKM2QUzuMnMFGfXa
名称:Testgroup 2
成员
0:123456789

成员数量未定义。
如何获得其中一个成员是 123456789



的每个组这个问题是不同的从这个,因为在另一个检查价值的关键实际上是已知的。

解决方案

我创建了NSObject类,并将其命名为FBNSArrayDB,在其中创建了Firebase ID, p>

  var name:NSString? 
var成员:NSString?
var 0:NSString?
var 1:NSString?

然后在您的UIViewController中执行此操作

 var fbarray = [FBNSArrayDB]()//在顶部

内部func

  let ref = FIRDatabase.database()。reference()。child(uid!)//假设你在uid点头下保存这个!观察事件类型(.ChildAdded,withBlock:{(snapshot)in 

如果让字典=快照.value as?[String:AnyObject] {
let xy = FBNSArrayDB()
xy.setValuesForKeysWithDictionary(dictionary)

guard let SomeV = xy.0

else {
print(NA)
return
}

如果SomeV ==123456789
{
self .fbarray.append(xy)//这样你可以使用Func之外的数组值
$ b print(xy.name)
print(xy.0)
print xy.1)
print(snapshot.key)//如果你想获得AutoID



}



},withCancelBlock:nil)


I have a data structure like this:

groups
  -KOrPKM2QUzuMnMlHfJu
    name: "Testgroup 1"
    members
      0: 123456789
      1: 987654321

  -KOrPKM2QUzuMnMFGfXa
    name: "Testgroup 2"
    members
      0: 123456789

The number of members is not defined. How can I get every group where one of the members is 123456789?

This question is different from this because in the other one the key of the value to check is actually known.

解决方案

I created NSObject class and called it FBNSArrayDB, inside it I created the firebase ID's in your case it will be:

   var name: NSString?
    var members: NSString?
    var 0: NSString?
var 1: NSString?

then do this in your UIViewController

var fbarray = [FBNSArrayDB]()//at the top

inside func

 let ref = FIRDatabase.database().reference().child(uid!)// I assume you are saving this under uid nod!



    ref.child("groups").observeEventType(.ChildAdded, withBlock: { (snapshot) in

        if let dictionary = snapshot.value as? [String: AnyObject]{
            let xy = FBNSArrayDB()
            xy.setValuesForKeysWithDictionary(dictionary)

            guard let SomeV = xy.0

                    else {
                        print ("NA")
                        return
                }

                if SomeV == "123456789"
{
self.fbarray.append(xy) //this way you can use the array values outside the Func

print (xy.name)
print (xy.0)
print (xy.1)
print (snapshot.key) // if you want to get the AutoID 



 }


        }
        }, withCancelBlock: nil)

这篇关于通过深层次的孩子的价值查询数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆