query & where in Firebase (swift 3)

你。 提交于 2020-01-02 12:45:26

问题


How i can make query in firebase? On oracle sql the query would be

"select * from users where username = "SomeUser"

I need to do the same in Firebase, Swift 3


回答1:


You need to use queryOrderedByChild and queryStartingAtValue. This should get you started in the right direction.

queryStartingAtValue(username).queryEndingAtValue(username)

let username = "SomeUser"

FIRDatabase.database().reference.child("users").queryOrderedByChild("username").queryStartingAtValue(username).queryEndingAtValue(username).observeEventType(.ChildAdded, withBlock: { (snapshot) -> Void in

}

queryEqual(toValue: username)

let searchRef = FIRDatabase.database().reference().child("users").queryOrdered(byChild: "username").queryEqual(toValue: username)



回答2:


Swift 3 && Firebase 3

I hope this will help you understand. Here we go

This is example of the data structure:

And here is the code:

    let username = "DavidMouse"

    // create searchRef or queryRef you name it
    let searchRef = FIRDatabase.database().reference().child("users").queryOrdered(byChild: "username").queryEqual(toValue: username)

    // search the username
    searchRef.observeSingleEvent(of: .value, with: { (snapshot) in

        guard snapshot.value is NSNull else {

            // yes we got the user
            let user = snapshot.value as! String
            print("\(user) is exists")
            return
        }

        // no there is no user with desire username
        print("\(username) is exists")
    }) { (error) in
        print("Failed to get snapshot", error.localizedDescription)
    }


来源:https://stackoverflow.com/questions/41038228/query-where-in-firebase-swift-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!