How to append nested data in firestore - ios

{
"userID": "MyID123"
"voteInfo": {
"docId1": 1
"docId2": 1
"docId3": 2
....
}
}
I would like to record which number the user voted for each two-point questionnaire. At first, only the user ID exists in the 'users' document, and I want to add data whenever I update it.
My code that is not working is as follows.
let userID = "MyID123"
let docID = "Ffeji341Fje3"
db.collection("users").document(userID).updateData([
"voteInfo": [
docID: 1
]
])

If you want to store the vote count for a number of keys:
Don't store the counts as an array, but store them in a map field.
Use the atomic increment operator to increase the vote count.
let key = "voteInfo."+docId
let update = [String:Any]
update[key] = FieldValue.increment(Int64(1))
db.collection("users").document(userID).updateData(update)

You can do this pretty easily through dot notation.
Given this Firestore structure
user_votes
uid_0
voteInfo //a map
docId1: 1
docId2: 1
docId3: 2
here's a function to append docId4: 1 to the existing docs
func addDoc() {
let collection = db.collection("user_votes").document("uid_0")
let newData = [
"voteInfo.docId4": 1
]
doc.updateData(newData, completion: { error in
if let err = error {
print(err.localizedDescription)
return
}
print("success")
})
}

Related

ios firebase how to fetch multiple object inside collection?

I had a problem with firebase , i have 5 document IDs . I need to query those 5 documents , convert them in to object.
for oneID in allIDs {
self.db.collection("storecollection").document(oneID).getDocument {(snap,err) in
let oneobject = convertToObject(snap)
self.tempHolder.append(oneobject)
var newarray = [MyObjectClass]()
if allIDs.last == oneID {
// perform copy
for x in 0...(self.tempHolder.count -1){
newarray.append(self.tempHolder[x])
}
self.tempHolder.removeAll()
completion(newarray)
}
}
Something wrong with code above , the size of self.tempHolder always count = 1. (Only the last id fetched object exist) i have no idea how to make it right.
Whats the right way to fetch multiple document (with specifiedID) ???
There's a bit of extraneous code in the question so it's not exactly clear but it seems you want to iterate over an array of document keys, read each associated document and add properties to an array (or in your case create an object based on those properties and add it)
Here's a simple example reading in a series of posts, and appending the post text from each post in an array.
The structure is
posts //a collection
post_0
post_text: "A post"
post_1
post_text: "Another post"
post_2
post_text: "Cool post"
and the code to read in post_0 and post_2 and append the post text to an array
var postTextArray = [String]()
func readMultiplePosts() {
let postKeyArray = ["post_0", "post_2"]
for postKey in postKeyArray {
let docRef = self.db.collection("posts").document(postKey)
docRef.addSnapshotListener { documentSnapshot, error in
guard let document = documentSnapshot else {
print("err fetchibng document")
return
}
guard let data = document.data() else {
print("doc was empty")
return
}
print("doc data: \(data)")
let post = document.get("post_text") as! String
self.postMsgArray.append(post)
}
}
}
then sometime later we want to print the post texts
for p in self.postMsgArray {
print(p)
}
and the output from console
A post
Cool post
While this solution works, a Firebaser will quickly point out that reading data like this in a tight loop is generally not recommended. It would be better to have some other correlation between the posts you want to read and then perform a query to read them in.

Firebase - Nested observeSingleEvent query in for loop callback too many time update

I am facing a serious callback hell on firebase realtime database update.
Situation:
I have comments node which store all the comment's detail information, such as belong to whose userId (uid) , message, and post id (pid). Please see image below.
I have another post-comment nodes, which store comments key under each post id key. Please see image below.
Finally the third nodes is user-comment, which store all comments key under unique user account id key. Please see image below.
Problem:
Everything work fine on "Write comment" function, because it just create a comment key and update comment data to these nodes.
But, when user call "Delete post" function, which will delete all the comments data belong to this post id. Therefore, I have this code logical to loop all the comments data. The whole point is that first I have to get the post-comment snapshot in order to limitation the query amount on comments node (because comments node store all the app user's comment detail data. Without knowing the quantity of comment belong to the target post, it will need to for loop all over the comments node, it is too overload.)
For looping the post-comment will get the commentKey, then I can set Null on comments node and post-comment node.
But the issues happen on I need to use comments node to find out the userId, in order to set NSNull on user-comment. When I calling the event below:
commentsRef.child((snap as AnyObject).key).observeSingleEvent(of:
.value, with: { (commentSnapshot) in
})
The commentsRef callback scope become another thread. Therefore, if I call rootRef.updateChildValues out side of this scope and in the end of for loop (post-comment) which will only update comments node and post-comment node. The user-comment updates data will still assign key:value on the other thread.
updates["user-comment/(userId)/comments/(commentKey)"] = NSNull()
I have to put the rootRef.updateChildValue in the
commentsRef.child((snap as AnyObject).key).observeSingleEvent(of:
.value, with: { (commentSnapshot) in
...
rootRef.updateChildValues(updates)
})
This logic will cause updateChildValues being called too many time if the comments over 10,000 or more than 1 million, because it is in the for looping. I use count down method try to call update only once on the for loop end. But the count number always be 0 in the commentRef scope... I don't know why...
Please help me out with a better solution to dealing with this nested observeSingleEvent update issues without changing the current nodes structure. My goal is to only call rootRef.updateChildValue one time.
Thanks for your help.
Demo code:
func deleteAllCommentsRelateTo(postId: String, callback: ((CommentServiceError?) -> Void)?) {
var error: CommentServiceError?
guard session.isValid else {
error = .authenticationNotFound(message: "Authentication not found.")
callback?(error)
return
}
let uid = session.user.id
let rootRef = Database.database().reference()
let path1 = "posts/\(postId)/comments_count"
let path2 = "posts/\(postId)/uid"
let commentCountRef = rootRef.child(path1)
let authorRef = rootRef.child(path2)
authorRef.observeSingleEvent(of: .value, with: { authorSnapshot in
guard let authorId = authorSnapshot.value as? String else {
error = .failedToDelete(message: "Author not found")
callback?(error)
return
}
if uid != authorId {
error = .failedToDelete(message: "User has no permission to delete this post comments")
callback?(error)
return
}
commentCountRef.runTransactionBlock({ (data) -> TransactionResult in
if let _ = data.value as? Int {
data.value = 0
}
return TransactionResult.success(withValue: data)
}) { (err, committed, snapshot) in
guard err == nil, committed else {
error = .failedToDelete(message: "Unable to delete a comment")
callback?(error)
return
}
var updates: [AnyHashable: Any] = [:]
/**
* [CHECKED] Set NSNull() on comments, post-comment, and user-comment nodes.
*/
let commentsRef = rootRef.child("comments")
let postCommentRef = rootRef.child("post-comment")
let query = postCommentRef.child(postId).child("comments").queryOrderedByKey()
query.observeSingleEvent(of: .value, with: { (data) in
guard data.hasChildren() else {
error = .failedToDelete(message: "No comments data")
callback?(error)
return
}
var count = data.childrenCount
print("post-comment count!!!!!!!: ", data.childrenCount)
for snap in data.children {
guard let commentKeySnap = snap as? DataSnapshot else {
continue
}
count -= 1
let commentKey = commentKeySnap.key
if count == 0 {
print("this is totally not right!!!!!")
}
updates["comments/\(commentKey)"] = NSNull()
updates["post-comment/\(postId)/comments/\(commentKey)"] = NSNull()
commentsRef.child((snap as AnyObject).key).observeSingleEvent(of: .value, with: { (commentSnapshot) in
guard let userId = commentSnapshot.childSnapshot(forPath: "uid").value as? String else {
return
}
updates["user-comment/\(userId)/comments/\(commentKey)"] = NSNull()
print("In this observeSingleEvent will always be 0 count::::: ", count)
if count == 0 {
rootRef.updateChildValues(updates, withCompletionBlock: { err, ref in
guard err == nil else {
error = .failedToDelete(message: "Failed to delete comment")
callback?(error)
return
}
})
print("deleteAllComments: ", updates)
callback?(nil)
}
})
print("count down: ", count)
}
})
})
}
})
}
Solution:
I accidentally found out the correct place to put count -= 1. Originally I put it in the for loop scope, but the count did not decrease in the commentRef in scope. Therefore, I put count -= 1 in the commentRef scope which success count to zero and only call rootRef.update one time.

Pagination in firebase with identical child values

My data structure is as follows:
users:
user1:
-carModel: evo x
-username: importguy
-region: north east
user2:
-carModel: evo x
-username: evoguy
-region: north east
user3:
-carModel: mustang gt
-username: muscleguy
-region: south east
I want the user to be able to search for a car, say evo, and display results of users who own those particular cars. I need to paginate these results for my app. The problem is I can't figure out how to properly query this. Here is what i have so far.
func fetchUsersBy(car: String) {
if self.carCurrentKey == nil {
let ref = USER_REF.queryOrdered(byChild: "carModel").queryStarting(atValue: car).queryLimited(toFirst: 3)
ref.observeSingleEvent(of: .value, with: { (snapshot) in
guard let snap = snapshot.children.allObjects as? [FIRDataSnapshot] else { return }
guard let last = snapshot.children.allObjects.last as? FIRDataSnapshot else { return }
snap.forEach({ (snapshot) in
guard let userDict = snapshot.value as? Dictionary<String, AnyObject> else { return }
guard let carModel = userDict["carModel"] as? String else { return }
if carModel.contains(car) {
print(snapshot)
}
})
self.carCurrentKey = last.key
self.carCurrentValue = last.childSnapshot(forPath: "carModel").value as? String
})
} else {
// where to start next query?
let ref = USER_REF.queryOrder(byChild: "carModel").queryStarting(atValue: self.carCurrentValue)
}
}
I have to order the query by carModel, in order to group all of the users with that particular car type together in a snapshot. Since all the car models are the same value, I cannot figure out where to start or end the next query for the pagination. Using the reference i have in the else block starts the query at the same place as the block above. Any help or advice would be much appreciated.
I considered doing a fan out, and making a separate structure for car types. This would be difficult though.
For both startAt() and endAt(), you can pass a second value, childKey as shown here.
So your query will look something like this:
let ref = USER_REF.queryOrdered(byChild: "carModel").queryStarting(atValue: self.carCurrentValue, childKey: self.carCurrentKey).queryLimited(toFirst: 3+1)
Note that I used toFirst: 3+1. That's because, annoyingly, startAt() is inclusive and there's no way to skip the first record. So, since we started from the last record retrieved on the previous page, you will want to query for one extra record and discard the first result.
Here's a more complete example in JavaScript. Not familiar enough to translate this to Swift, but it should give you the algorithm in completion.
class Cursor {
constructor(baseRef, pageSize) {
this.baseRef = baseRef;
this.lastKey = null;
this.lastValue = null;
this.pageSize = pageSize;
}
next() {
let ref = this.baseRef;
if( this.lastValue !== null ) {
// a previous page has been loaded so get the next one using the previous value/key
// we have to start from the current cursor so add one to page size
ref = ref.startAt(this.lastValue, this.lastKey).limitToFirst(this.pageSize+1);
}
else {
// this is the first page
ref = ref.limitToFirst(this.pageSize);
}
return ref.once('value').then(snap => {
const keys = [];
const data = []; // store data in array so it's ordered
snap.forEach(ss => {
data.push(ss.val());
keys.push(ss.key);
});
if( this.lastValue !== null ) {
// skip the first value, which is actually the cursor
keys.shift();
data.shift();
}
// store the last loaded record
if( data.length ) {
const last = data.length - 1;
this.lastKey = keys[last];
this.lastValue = data[last].author;
}
return data;
});
}
}
And here's a working fiddle.
Keep in mind that this is a realtime data stream. So pagination is tricky. It's generally easier to just do infinite scroll than to try and maintain a realistic cursor on a moving data set (records can reorder when data changes, get deleted, added in the middle, etc).

Firebase queryOrderedByChild() method not giving sorted data

My database structure is some thing like this:
{
"users": {
"alovelace": {
"name": "Ada Lovelace",
"score": 4,
},
"ghopper": { ... },
"eclarke": { ... }
}
}
I am trying to retrieve top 20 scores in descending order.
let queryRef = FIRDatabase.database().reference().child("users").queryOrderedByChild("score").queryLimitedToLast(20)
queryRef.observeSingleEventOfType(.Value, withBlock: { (querySnapShot) in
print(querySnapShot.value)
})
i am trying to get output like
score": 4
score": 3
score": 2
or
score": 2
score": 3
score": 4
or
2
3
4
Please let me know how to solve this.
When you request the children in a specific order, the resulting snapshot will contain both the data that matches the query and information about the order in which you requested them.
But when you request the .value of the snapshot, the keys+data are converted to a Dictionary<String,AnyObject>. Since a dictionary does not have an extra place to put the information about the order, that information is lost when converting to a dictionary.
The solution is to not convert to a dictionary prematurely and instead loop over the snapshot:
queryRef.observeSingleEventOfType(.Value, withBlock: { (querySnapShot) in
for childSnapshot in querySnapShot.children {
print(childSnapshot.value)
}
})
You can also listen to the .ChildAdded event, instead of .Value, in which case the children will arrive in the correct value:
queryRef.observeSingleEventOfType(.ChildAdded, withBlock: { (childSnapshot) in
print(childSnapshot.value)
})
Update
I just added this JSON to my database:
{
"users" : {
"alovelace" : {
"name" : "Ada Lovelace",
"score" : 4
},
"eclarke" : {
"name" : "Emily Clarke",
"score" : 5
},
"ghopper" : {
"name" : "Grace Hopper",
"score" : 2
}
}
}
And then ran this code:
let queryRef = ref.child("users").queryOrderedByChild("score").queryLimitedToLast(20);
queryRef.observeEventType(.ChildAdded) { (snapshot) in
print(snapshot.key)
}
The output is:
ghopper
alovelace
eclarke
Which is the users in ascending order of score.
Update to add more on getting the scores in descending order
The above code gets the 20 highest scores in ascending order. There is no API call to return themthem in descending score.
But reversing 20 items client side is no performance concern, you just need to write the code for it. See for example this answer.
If you really are stuck on reversing them client side, you can add an inverted score. See this answer for an example of that.
Use method observeEventType instead of observeSingleEventOfType.
Also, make FIRDataEventType to ChildAdded.
Last, If you want Top 20 items, use queryLimitedToFirst instead of queryLimitedToLast.
{
"users" : {
"alovelace" : {
"name" : "Ada Lovelace",
"score" : 4
},
"eclarke" : {
"name" : "Emily Clarke",
"score" : 5
},
"ghopper" : {
"name" : "Grace Hopper",
"score" : 2
}
}
}
For the dataset above
let queryRef = FIRDatabase.database().reference().child("users").queryOrderedByChild("score").queryLimitedToFirst(20)
queryRef.observeEventType(.ChildAdded, withBlock: { (snapshot) in
print("key: \(snapshot.key), value: \(snapshot.value)")
})
key: ghopper, value: Optional({
name = Grace Hopper;
score = 2;
})
key: alovelace, value: Optional({
name = Ada Lovelace;
score = 4;
})
key: eclarke, value: Optional({
name = Emily Clarke;
score = 5;
})
Snapshot will returns the contents as native types.
Data types returned:
NSDictionary
NSArray
NSNumber (also includes booleans)
NSString
So, you can get your scores this way.
let queryRef = FIRDatabase.database().reference().child("users").queryOrderedByChild("score").queryLimitedToFirst(20)
queryRef.observeEventType(.ChildAdded, withBlock: { (snapshot) in
if let scores = snapshot.value as? NSDictionary {
print(scores["score"])
}
})
Optional(2)
Optional(4)
Optional(5)
Moreover, the default of realtime database return everything in ascending order.
If you want descending order, you can make some tricks(4:40) in your database.

Looping JSON from Parse database giving wrong results

Swift 2.0, xcode 7.1
I am trying to retrieve some data from Parse database, filter it to remove duplicate and store in a dictionary. Each row of parse has orders placed by customer (JSON shown below) and I want to retrieve this in UITableView to show the order placed. If the customer has placed multiple orders recently, I want to filter that and show all of his orders in one section of table view under his customer ID.
Filtering is working, but for some reason my loop is not giving me accurate results.
Parse Row 1:
[{"Customer":"9sKSDTG7GY","Product":"Burger","Quantity":"2"}]
Parse Row 2:
[{"Customer":"nyRHskbTwG","Product":"Sizzler","Quantity":"2"},{"Customer":"nyRHskbTwG","Product":"Biryani","Quantity":"2"}]
Retrieved this data and stored in self.custome, self.fQuantity and self.fName variable.
The loop I am using is as below:
let cD = self.customer
print("Customer data before filtering Unique value: \(self.customer)")
self.uniqueValues = self.uniq(cD) //Calling a function to get unique values in customer data
print("Customer data after filtering Unique value: \(self.uniqueValues)")
var newArray = [[String]]()
for var count = 0; count < self.customer.count; count++ {
for sID in self.uniqueValues {
if sID.containsString(self.customer[count]){
let dicValue = [String(self.fQuantity[count]), String(self.fName[count])]
newArray.append(dicValue)
self.dicArray.updateValue(newArray, forKey: sID)
} else {
// Do nothing...
}
}
}
print("Dictionary Values: \(Array(self.dicArray.values))")
print("Dictionary Keys: \(Array(self.dicArray.keys))")
Printed output is as below:
Customer data before filtering Unique value: ["9sKSDTG7GY",
"nyRHskbTwG", "nyRHskbTwG"]
Customer data after filtering Unique value: ["9sKSDTG7GY",
"nyRHskbTwG"]
Dictionary Values: [[["2", "Burger"], ["2", "Sizzler"],
["2", "Biryani"]], [["2", "Burger"]]]
Dictionary Keys: ["nyRHskbTwG", "9sKSDTG7GY"]
Can someone figure out what I am doing wrong?
As #David suggested, you have to swap outer and inner loop. But I also had to delete all values contained in newArray if there was nothing found in the if loop. So this is how I made it work.
let cD = self.customer
print("Customer data before filtering Unique value: \(self.customer)")
self.uniqueValues = self.uniq(cD) //Calling a function to get unique values in customer data
print("Customer data after filtering Unique value: \(self.uniqueValues)")
var newArray = [[String]]()
for sID in self.uniqueValues {
for var count = 0; count < self.customer.count; count++ {
if sID.containsString(self.customer[count]){
let dicValue = [String(self.fQuantity[count]), String(self.fName[count])]
newArray.append(dicValue)
self.dicArray.updateValue(newArray, forKey: sID)
} else {
newArray.removeAll() // ****** Adding this works for me
}
}
}
print("Dictionary Values: \(Array(self.dicArray.values))")
print("Dictionary Keys: \(Array(self.dicArray.keys))")
You filtered the data, but are looping through the un-filtered list of customers.
for var count = 0; count < self.customer.count; count++ {

Resources