Swift Firebase查询孩子的孩子

时间:2017-08-17 11:20:26

标签: swift firebase firebase-realtime-database

所以我在这里停留了半天,我试图获得 Arya 的所有主题,但我很难做到这一点。

这是Firebase中的示例结构。

var obs = [
    { from: "A", to: "C", dis: 5 },
    { from: "A", to: "D", dis: 4 },
    { from: "D", to: "C", dis: 8 },
    { from: "C", to: "B", dis: 9 },
    { from: "B", to: "D", dis: 17 },
];
function findPath(from, to) {
    var open = obs
        .filter(function (part) {
        return part.from == from || part.to == from;
    });
    var dict = {};
    dict[from] = { dis: 0, path: from };
    obs
        .filter(function (part) {
        return part.from == from || part.to == from;
    })
        .forEach(function (v, i) {
        if (dict[v.to] === void 0) {
            dict[v.to] = { dis: v.dis, path: from + v.to };
        }
        else if (dict[v.to].dis > v.dis) {
            dict[v.to] = { dis: v.dis, path: from + v.to };
        }
    });
    while (dict[to] === void 0) {
        for (var key in dict) {
            if (dict.hasOwnProperty(key)) {
                var closed = dict[key];
                obs
                    .filter(function (part) {
                    return part.from == key || part.to == key || part.from == key || part.to == key;
                })
                    .forEach(function (v, i) {
                    var c = v.dis + closed.dis;
                    if (dict[v.to] === void 0) {
                        dict[v.to] = { dis: c, path: closed.path + v.to };
                    }
                    else if (dict[v.to].dis > c) {
                        dict[v.to] = { dis: c, path: closed.path + v.to };
                    }
                    if (dict[v.from] === void 0) {
                        dict[v.from] = { dis: c, path: closed.path + v.to };
                    }
                    else if (dict[v.from].dis > c) {
                        dict[v.from] = { dis: c, path: closed.path + v.to };
                    }
                });
            }
        }
    }
    return dict[to];
}
//TEST
var path = findPath("A", "B");
console.log(path);

然后我找到了这个Querying in Firebase by child of child

我试过这个

-Subjects
    -math
        id: 1
        name: Math
        -students
            -Arya
                id: 1
                name: Arya
            -JonSnow
                id: 2
                name: JonSnow
            +justsomename
    +science
    +english
    +history
    +computer
  

ref ==主题

但这是回归

ref.queryOrdered(byChild: "students/name").queryEqual(toValue: "Arya").observeSingleEvent(of: .value, with: { snapshot in

        print(snapshot)
    })
  

查询是否正确,我只是做错了什么?

1 个答案:

答案 0 :(得分:-1)

也许像这样......可以起作用......

var subjects = [String]()

    ref.observeSingleEvent(of: .value, with: { snapshot in

        let value = snapshot.value as? NSDictionary
        let postsIds = value?.allKeys as! [String] // this will put all the subjects such as math into an array called postIDs

        for postId in postsIds { //this is going to cycle through the array and check each one for your student
            let refToPost = Database.database().reference(withPath: "Subjects" + postId)
            refToPost.observeSingleEvent(of: .value, with: { snapshot in
                if snapshot.hasChild("Arya") {



                    self.subjects.append(snapshot)
                }
            })
        }
    })
  print("this is the ist of subjects with my student: \(subjects)")