Firebase iOS - 使用布尔运算符获取数据

时间:2017-02-17 00:32:28

标签: swift firebase firebase-realtime-database

我正在尝试使用firebase for iOS,我遇到了问题。

我正在使用以下JSON树:

{
  "expressions" : {
    "expr0" : {
      "id" : 0,
      "text" : "Text0"
    },
    "expr1" : {
      "id" : 1,
      "text" : "Text1"
    },
    "expr2" : {
      "id" : 2,
      "text" : "Text2"
    },
    "expr3" : {
      "id" : 3,
      "text" : "Text3"
    }
  },
  "levels" : {
    "level0" : {
      "coverImage" : "lvl0",
      "expressions" : {
        "expr0" : true,
        "expr2" : true
      },
      "id" : 0,
      "title" : "Level0"
    },
    "level1" : {
      "coverImage" : "lvl1",
      "expressions" : {
        "expr0" : true,
        "expr1" : true,
        "expr3" : true
      },
      "id" : 1,
      "title" : "Level1"
    },
    "level2" : {
      "coverImage" : "lvl2",
      "expressions" : {
        "expr1" : true,
        "expr2" : true,
        "expr3" : true
      },
      "id" : 2,
      "title" : "Level2"
    },
    "level3" : {
      "coverImage" : "lvl3",
      "expressions" : {
        "expr3" : true
      },
      "id" : 3,
      "title" : "Level3"
    }
  }
}

我有两种对象,表达式和级别。以下是这两个对象的数据模型:

struct Level {
    let id : Int
    let coverImage : String
    let title : String
    let expressions : [Expression]
}

struct Expression {
    let id : Int
    let expression : String
}

我还有一个DataService单例来为我提供Firebase数据库参考:

    import UIKit
    import FirebaseDatabase

    class DataService {

        private static let _instance = DataService()

        static var instance: DataService {
            return _instance
        }

        var mainRef: FIRDatabaseReference {
            return FIRDatabase.database().reference()
        }

        var levelsRef: FIRDatabaseReference {
            return mainRef.child("levels")
        }

        var expressionsRef: FIRDatabaseReference {
            return mainRef.child("expressions")
        }

    }

我希望在Levels数组中存储与其对应表达式相关的级别,并使用collectionView显示此数组。根据JSON文件,最终结果必须是:

levelsArr[Level(id: 0, coverImage: "lvl0", title: "Level0", expressions: [Expression(id: 0, expression: "Text0"), Expression(id: 2, expression: "Text2")]), Level(id: 1, coverImage: "lvl1", title: "Level1", expressions: [Expression(id: 0, expression: "Text0"), Expression(id: 1, expression: "Text1"), Expression(id: 3, expression: "Text3")]), Level(id: 2, coverImage: "lvl2", title: "Level2", expressions: [Expression(id: 1, expression: "Text1"), Expression(id: 3, expression: "Text3"), Expression(id: 2, expression: "Text2")]), Level(id: 3, coverImage: "lvl3", title: "Level3", expressions: [Expression(id: 3, expression: "Text3")])]

以下是我目前的以下功能:

    private var levelsArr = [Level]()   

    func fetchLevels() {

        DataService.instance.levelsRef.observeSingleEvent(of: .value, with: { (snapshot) in
            if let levels = snapshot.value as? Dictionary<String, AnyObject> {

                for(_, valueLvl) in levels {

                    if let dictLvl = valueLvl as? Dictionary<String, AnyObject> {

                        if let id = dictLvl["id"] as? Int, let coverImage = dictLvl["coverImage"] as? String, let title = dictLvl["title"] as? String {

                            var expressionsArr = [Expression]()
                            if let expr = dictLvl["expressions"] as? Dictionary<String, AnyObject> {
                                for(keyLvlExp, _) in expr {
                                    DataService.instance.expressionsRef.observeSingleEvent(of: .value, with: { (snapshot) in
                                        print(snapshot)
                                        if let expressions = snapshot.value as? Dictionary<String, AnyObject> {
                                            for (keyExpr, valueExpr) in expressions {
                                                if keyLvlExp == keyExpr {
                                                    if let dictExpr = valueExpr as? Dictionary<String, AnyObject> {
                                                        if let id = dictExpr["id"] as? Int, let text = dictExpr["text"] as? String{
                                                            let expression = Expression(id: id, expression: text)
                                                            expressionsArr.append(expression)
                                                        }
                                                    }
                                                }

                                            }
                                        }
                                        let level = Level(id: id, coverImage: coverImage, title: title, expressions: expressionsArr)
                                        self.levelsArr.append(level)
                                        print(level)
                                    })

                                }
                            }

                        }
                    }
                }
            }
        })

        DispatchQueue.main.async{
                self.collectionView?.reloadData()
        }

    }

在viewDidLoad()中调用函数fetchLevels()。

打印(快照)的结果是:

Snap (expressions) {
    expr0 =     {
        id = 0;
        text = Text0;
    };
    expr1 =     {
        id = 1;
        text = Text1;
    };
    expr2 =     {
        id = 2;
        text = Text2;
    };
    expr3 =     {
        id = 3;
        text = Text3;
    };
}

谢谢。

0 个答案:

没有答案