Firebase: query and delete keys in a range - swift

If I have some data in the Firebase real-time database like this
root--.
|--stuff--.
|--1
|--2
|--3
|--4
|--5
|--6
|--7
where all those numbers are keys that contain more data, and I want to delete all the keys less than or equal to 4, how do I do that without downloading the entire "stuff" branch?
With swift I think I can query the keys in that range by
let ref = FIRDatabase.database().reference().child("stuff")
let query = ref.queryEnding(atValue: 4)
but I don't know how to retrieve the key names themselves so that I can delete them sort of like this pseudocode
for key in queryResults {
let ref = FIRDatabase.database().reference().child("stuff/\(key)")
ref.setValue(nil)
}
(In reality I'm dealing with timestamp keys and I want to delete data branches that have gotten too old.)

Not sure how to do it in swift, but you can do it with orderByKey.
.database().reference()
.child("stuff")
.orderByKey()
.startAt(STARTING_TIMESTAMP)
.endAt(ENDING_TIMESTAMP)
And then loop over the ids of the corresponding result.
This will download all information for those children though. If you don't want that you'll have to store it somewhere else to easily delete it.

Sample Swift 3 / Firebase 2.x code to get the keys from a snapshot
for child in (snapshot?.children)! {
let snap = child as! FDataSnapshot
print(snap.key)
}

Related

Retriving data from database

First time asking a question here, so sorry if I do it wrong.
Anyways. I'm using Firebase Database to store "Results" in my Quiz app. When data is store it looks like this
Results
-LjQ34gs7QoL1GMufiMsaddclose
Score: xx
UserName: xx
-LjQ3NeCoDGob8wnhstH
Score: xx
UserName: xx
I would like to access score and username from it and display it in a HighScore tableview. Problem is - I can get the "Results" node, but because of the id of the results (ie LjQ34gs7QoL1GMufiMsaddclose) I don't know how to access the score and username.
I got the data snapshot​, but not sure how to "bypass" the id to get to score and username.
Hope I made it at least a bit clear, what the problem is.
let ref = Database.database().reference().child("Results")
ref.observe(.value) { (DataSnapshot) in
print(DataSnapshot.value as Any)
}
You current code gets you a single snapshot with the results of all users. You'll need to loop over the child snapshots to get the result of each user, and then look up their specific properties with childSnapshot(byName:):
let ref = Database.database().reference().child("Results")
ref.observe(.value) { (snapshot) in
for case let userSnapshot as DataSnapshot in snapshot.children {
print(userSnapshot.childSnapshot(forPath: "Score").value)
}
}
Also see:
How to get all child data from firebase without knowing the path
Iterate through nested snapshot children in Firebase using Swift
How do I loop through and get all the keys of the nested nodes in firebase?
Retrieving Data using Firebase Swift
And probably some more from this list.

Realm Swift - How to remove an item at a specific index position?

I am storing a simple list of id's as GUIDs in Realm, but would like the ability to delete an object at a particular index position.
So for example, I want to remove 04b8d81b9e614f1ebb6de41cb0e64432 at index position 1, how can this be achieved? Do I need to add a primary key, or is there a way to remove the item directly using the given index position?
Results<RecipeIds> <0x7fa844451800> (
[0] RecipeIds {
id = a1e28a5eef144922880945b5fcca6399;
},
[1] RecipeIds {
id = 04b8d81b9e614f1ebb6de41cb0e64432;
},
[2] RecipeIds {
id = cd0eead0dcc6403493c4f110667c34ad;
}
)
It seems like this should be a straightforward ask, but I can't find any documentation on it. Even a pointer in the right direction would do.
Results are auto-updating and you cannot directly modify them. You need to update/add/delete objects in your Realm to effect the state of your Results instance.
So you can simply grab the element you need from your Results instance, delete it from Realm and it will be removed from the Results as well.
Assuming the Results instance shown in your question is stored in a variable called recipes, you can do something like the following:
let recipeToDelete = recipes.filter("id == %#","04b8d81b9e614f1ebb6de41cb0e64432")
try! realm.write {
realm.delete(recipeToDelete)
}

How to know which index a child was added to an ordered FirebaseQuery

Using Firebase and Swift SDK
I just started with Firebase and wanted to display a list of Conversations ordered by last_update. The following query works fine :
let query = Database.database().reference().child("chat").child("channels").queryOrdered(byChild: "last_update")
query.observe(DataEventType.childAdded) { (snapshot: DataSnapshot) in
if let value = snapshot.value as? [String: Any?] {
log.debug("added channel: \(snapshot.key) : \(value)")
//add object to array, insertRow in tableview
}
}
The first time my view is loaded, each item arrives in the correct order specified by the query, so the display is ok. But if I create a new channel, it does appear at the end of the tableview, because I just add it at the end of the array and just call insertRow on my table view. My question is : is there any mecanism that give us the new inserted position of the DataSnapshot ?
Same question for DataEventType.childMoved : we get to know that a snapshot has moved, but how to know where it has moved ??
I finally end up using FirebaseUI, especially the submodule FirebaseDatabaseUI (see the github repo here)
They provide a FUITableViewDataSource, you just create a query and pass it to it, it will handle everything like sorting etc. I also used FUIArray, which is simply an array backed by a query, with a delegate for added / deleted / moved / update events (and proper indexes).

Query latest entries using Firebase in Swift

I'm new using Firebase and I can't find how to do what sounds really simple to do : list all the latest entries of my database.
Here is a screenshot of what my database looks like :
So, I'm trying to list the latest entries like that :
// picturesRef = FIRDatabase.database().reference().child("pictures")
let _ = self.picturesRef.queryOrdered(byChild: "createdTime").queryLimited(toLast: 50).observe(.value, with: { snapshot in
// Stocking the result into picturesArr array
for elem in picturesArr {
print(elem.createdTime)
}
})
And right now, when I'm displaying the createdTime value of each item, I have something like :
1484738582.0
1484000086.0
1484738279.0
1484734358.0
1484625525.0
1484728677.0
Which doesn't seem to be ordered from the oldest entry to the newest one...
Also, when I replace "createdTime" in the query by "fieldThatDoesntExist", I have the exact same result.
Anyone would know where did I do something wrong in the code ? Thanks in advance !
The query returns the items in the correct order. But most likely (the relevant code seems to be missing from your question) you're losing that order when you convert the snapshot to a dictionary (which is unordered by definition).
To keep the items in the correct order, iterate over snapshot.children:
let picturesRef = FIRDatabase.database().reference().child("pictures")
let _ = picturesRef
.queryOrdered(byChild: "createdTime")
.queryLimited(toLast: 50)
.observe(.value, with: { snapshot in
for child in snapshot.children {
print(child.key)
print(child.child("createdTime").value
}
})
Also see:
Firebase snapshot.key not returning actual key?
Firebase access keys in queryOrderBy
Retrieving Data using Firebase Swift
Iterate over snapshot children in Swift (Firebase) (I usually try to avoid allObjects, but it's fine too)
Actualy, after sorting the array returned by the query with this :
picturesArr.sort(by: {$0.createdTime > $1.createdTime})
I've figured out that the query returns the entries that I'm looking for but not sorted.
It looks a bit wierd to me, maybe someone knows why or even better, how to get the result already sorted ?

Querying in Firebase by child of child

I have a structure of objects in Firebase looking like this:
-KBP27k4iOTT2m873xSE
categories
Geography: true
Oceania: true
correctanswer: "Yaren (de facto)"
languages: "English"
question: "Nauru"
questiontype: "Text"
wronganswer1: "Majuro"
wronganswer2: "Mata-Utu"
wronganswer3: "Suva"
I'm trying to find objects by categories, so for instance I want all objects which has the category set to "Oceania".
I'm using Swift and I can't really seem to grasp the concept of how to query the data.
My query right now looks like this:
ref.queryEqualToValue("", childKey: "categories").queryOrderedByChild("Oceania")
Where ref is the reference to Firebase in that specific path.
However whatever I've tried I keep getting ALL data returned instead of the objects with category Oceania only.
My data is structured like this: baseurl/questions/
As you can see in the object example one question can have multiple categories added, so from what I've understood it's best to have a reference to the categories inside your objects.
I could change my structure to baseurl/questions/oceania/uniqueids/, but then I would get multiple entries covering the same data, but with different uniqueid, because the question would be present under both the categories oceania and geography.
By using the structure baseurl/questions/oceania/ and baseurl/questions/geography I could also just add unique ids under oceania and geography that points to a specific unique id inside baseurl/questions/uniqueids instead, but that would mean I'd have to keep track of a lot of references. Making a relations table so to speak.
I wonder if that's the way to go or? Should I restructure my data? The app isn't in production yet, so it's possible to restructure the data completely with no bigger consequences, other than I'd have to rewrite my code, that pushes data to Firebase.
Let me know, if all of this doesn't make sense and sorry for the wall of text :-)
Adding some additional code to Tim's answer for future reference.
Just use a deep query. The parent object key is not what is queried so it's 'ignored'. It doesn't matter whether it's a key generated by autoId or a dinosaur name - the query is on the child objects and the parent (key) is returned in snapshot.key.
Based on your Firebase structure, this will retrieve each child nodes where Oceania is true, one at a time:
let questionsRef = Firebase(url:"https://baseurl/questions")
questionsRef.queryOrderedByChild("categories/Oceania").queryEqualToValue(true)
.observeEventType(.ChildAdded, withBlock: { snapshot in
print(snapshot)
})
Edit: A question came up about loading all of the values at once (.value) instead of one at at time (.childAdded)
let questionsRef = Firebase(url:"https://baseurl/questions")
questionsRef.queryOrderedByChild("categories/Oceania").queryEqualToValue(true)
.observeSingleEventOfType(.Value, withBlock: { snapshot in
print(snapshot)
})
Results in (my Firebase structure is a little different but you get the idea) uid_1 did not have Oceania = true so it was omitted from the query
results.
Snap (users) {
"uid_0" = {
categories = {
Oceania = 1;
};
email = "dude#thing.com";
"first_name" = Bill;
};
"uid_2" = {
categories = {
Oceania = 1;
};
"first_name" = Peter;
};
}
I think this should work:
ref.queryOrderedByChild("categories/Oceania").queryEqualToValue(true)