Querying Firebase with Swift 3.0
You need to use queryOrderedByChild
to createdAt
and than use equalTo Today
let ref = FIRDatabase.database().reference().child("thoughts").queryOrdered(byChild: "createdAt").queryEqual(toValue : "Today")
ref.observe(.value, with:{ (snapshot: FIRDataSnapshot) in
for snap in snapshot.children {
print((snap as! FIRDataSnapshot).key)
}
})
Firebase query in iOS swift
after read firebase documentation, I got my Answer where i made mistake.
here what i was done. i just replace queryEqual instance of child
refhandel = ref.child("SongPlay").queryOrdered(byChild:"post_key").queryEqual(toValue: post_key[myIndex]).observe(.childAdded, with: { (snapshot) in
if let item = snapshot.value as? [String: AnyObject]{
let music = MusicDataModel()
music.setValuesForKeys(item)
MusicData.append(music)
}
})
Query Firebase nested data to check specific value - iOS Swift
When you perform a query, Firebase compares a property of each child node under the location you query to the value you pass in.
I assume that profilesRef
points to /profiles
in your database. In that case profilesRef.child(user)
in your code points to the profile for a specific user already. When you then run a query, you are querying each of the properties of that specific user.
What you like want to do is to get all users with gender
equal to Female
. In that case you should query the user list:
DatabaseService.shared.profilesRef.queryOrdered(byChild: "gender").queryEqual(toValue: "Female")...
Note that you also should define the index on the level where the query is run, so on /users
:
"profiles": {
".indexOn": ["gender"]
}
Another alternative (the might scale better) is to not use a query altogether, but instead load the gender
property of each individual user in your array. In (approximate) code:
for user in nearbyUserArray {
DatabaseService.shared.profilesRef.child(user).child( "gender").observeSingleEvent(of: .value) { [weak self] (snapshot) in
if snapshot.value == "Female" {
print("snap: \(snapshot)")
}
}
}
query & where in Firebase (swift 3)
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)
Firebase query on multiple filters in Swift
The code you show shouldn't event compile. Firebase Database queries can only contain a single orderBy...
clause, and can never do suffix matching (your _NCAA
is at the end of the key).
To allow you use-case you should have a key/property that consists of the week number, and the org (NCAA
/NFL
) value. E.g.
Games
--- 20190220000_NCAA (SportID)
--- WeekId : 1
--- WeekOrg : "01_NCAA"
--- etc...
--- 20190221000_NCAA
--- WeekId : 2
--- WeekOrg : "02_NCAA"
--- etc...
--- 20190204000_NFL
--- WeekId : 1
--- WeekOrg : "01_NFL"
--- etc...
--- etc...(SportsID)
With this in place you can get the nodes in week 1 for the NCAA with:
gamesRef.queryOrdered(byChild: "WeekOrg").queryEqual(toValue: "01_NCAA").observe(.value, with: { snapshot in
Also see my answer here: http://stackoverflow.com/questions/26700924/query-based-on-multiple-where-clauses-in-firebase
How to make Firebase Database Query to get some of the children using Swift 3?
As El Capitain said, you'll need to use this:
func loadData(){
let ref = FIRDatabase.database().reference().child("Posts")
ref?.queryOrdered(byChild: "Des").queryEqual(toValue: "11").observe(.childAdded, with: { snapshot in
if let post = snapshot.value as? [String : AnyObject] {
// do stuff with 'post' here.
}
})
}
Also, as he mentioned, you'll want to set your security rules to allow you to search by 'Des', or whatever other child node you'll be querying by. In addition, you'll need to set the rules to allow you read access to the query location:
{
"rules": {
".read": "auth != null",
".write": "auth != null",
"Posts": {
"$someID": {
".indexOn": ["Des"]
}
}
}
}
However, you're not going to get 'only the images' though, your query will return the entire node. In this case, the query above would return the rrrrrr node.
iOS Firebase -What's the best way to query several levels down? 'InvalidQueryParameter', reason: 'Cannot use multiple queryOrderedBy calls!'
I am going to throw this out as a possible solution.
It's not exactly clear why the current structure is what it is, but I would suggest changing it to
post_comments
post_id_x
posted_by: "uid1234"
comment: "I like pizza"
if the structure needs to stay as is, then simply add this as another node which will make your comment queries much simpler.
Retrieve Data from Firebase using where Query in Swift 4
You need to add a query:
ref.queryOrdered(byChild: "videoid").queryEqual(toValue: "12345").observeSingleEvent(of: .value, with: { snapshot in
more info here:
https://firebase.google.com/docs/reference/swift/firebasedatabase/api/reference/Classes/DatabaseQuery#queryorderedbychild
Related Topics
Xcode 7 Supporting Watch Os1 and Os2
Confused About Orthographic Projection of Camera in Scenekit
Xcode 8.1 Push Notifications in Swift 2.3 with Firebase Integration Not Getting
Playing Back Audio Using Avaudioplayer iOS 7
Uikeyboardboundsuserinfokey Is Deprecated, What to Use Instead
Locking a Uisearchbar to the Top of a Uitableview Like Game Center
Connect Outlet of a Cell Prototype in a Storyboard
How to Split String into Substrings on iOS
Swift Protocol Get Only Settable
Uiprogressview Custom Track and Progress Images in iOS 7.1
Split View Controller Must Be Root View Controller
iOS 6.X Open Command Line on Jailbreak
How to Use Call Directory Extension to Identify a Incoming Call in My Application
Why Is This Code Not Recognising the Nsstring as Being Equal
How to Check Version of a Cocoapods Framework
Face Recognition on the Iphone
Save and Retrieve Value via Keychain
Fatal Error: Init(Coder:) Has Not Been Implemented Error Despite Being Implemented