如何在Swift中使用Firebase进行查询以获取多个queryOrderedByChild

时间:2016-09-19 06:09:55

标签: ios swift firebase firebase-realtime-database

如何获取其中username = current user和isCompleted = false的帖子的值。

    override func viewWillAppear(animated: Bool) {
    // How can I make a query to get the post along with the current user and the isCompleted = false.
    let postRef = FIRDatabase.database().reference().child("posts").queryOrderedByChild("username").queryEqualToValue(FIRAuth.auth()!.currentUser!.displayName!)
    // If I write the query like below if it will through error
    // let postRef = FIRDatabase.database().reference().child("posts").queryOrderedByChild("username").queryEqualToValue(FIRAuth.auth()!.currentUser!.displayName!).queryOrderedByChild("isCompleted").queryEqualToValue(false)


    postRef.observeEventType(.Value, withBlock: { (snapshot) in



        var newPosts = [TodoItemDatabase]()
        for post in snapshot.children{



            let post = TodoItemDatabase(snapshot: post as! FIRDataSnapshot)
            newPosts.insert(post, atIndex: 0)

        }
        toDoList = newPosts

        dispatch_async(dispatch_get_main_queue(), {
            self.toDoListTable.reloadData()
        })
    }) { (error) in
        print(error.localizedDescription)
    }

}

以下是帖子的数据结构

enter image description here

0 个答案:

没有答案