Firebase delete query in iOS swift - ios

I have list of comments in post.
On adding comments to post node , I have used childbyautoid().
and I have set commenttext, timestamp and userid as values for comment.
CASE:
I want to delete my comment in the post.
Problem:
How can I delete the comment according to userid = myuserid?
Went through many examples but couldn't found any?

You have to query all the node in comments. After querying all the autoId's in the node, you have to check for user id in each snapshot.
In that snapshot, you have to compare your user id and if it matches, you have take the autoId of that snapshot and delete it.

Related

Is my User ID supposed to be different than my Document ID for each User in my Users Collection (Firestore)?

I know that this is probably a dumb question, but I'm only starting to learn Firebase/Firestore and I'm having trouble finding the answer that I'm looking for.
I know that I can get the User ID from the currentUser method, but how am I supposed to reference the uid in my file path if the uid and the document id for each user is a different value?
For example, in the reference variable bellow, "HEo4YjfjJ0p2B6hVJRXs" is the document id. If I replace this value with the current user's uid, would it still work the same despite the fact that the uid value is different?
var colRef = Firestore.firestore().collection("/users/HEo4YjfjJ0p2B6hVJRXs/Days")
For example, in the reference variable bellow, "HEo4YjfjJ0p2B6hVJRXs" is the document id.
If that's the id of the document, than that id should be used in your reference, as you already do.
If I replace this value with the current user's uid, would it still work the same despite the fact that the uid value is different?
If the current user's uid is different than the id of the document, then you won't be able to read that document. If you need a document that has as id, the user's uid then you should create that document. If you are also thinking about wildcards, please also note that in Firestore there are no wildcards paths to documents. You have to identify collections and documents by their ids.

Firebase real time updates for posts from people I follow

I'm trying to get real time updates working with Firestore on my app. However, I'm not sure if what I'm trying to do is possible in real time.
I have Users who follow other Users. When they post to the Posts collection, the users timeline should be updated in real time with the posts from users they follow. Is this possible?
I have an identifier userId on each post. Any help would be appreciated as I'm a bit stuck!
You can fetch list of users that a user follows and
let queryRef = self.ref.child("posts/\(userID)")
queryRef?.observe(.childAdded, with: { (snapshot) in
// new post from a friend
})
Suppose a user john is logged In. First of all get userIDs of all the persons whom john is following. Store the userIDs in an array.
Than use an observer to listen for changes in the posts table. Whenever value of posts table is changed,
check the userID of that post.
if the array contains this userID. update the news feed of the logged In User.
Hope that helps.

Remove an entire child from Firebase using Swift

I'd like to write a function to remove an entire child from Firebase. I'm using XCode 10 and swift 3+.
I have all the user info of the child I'd like to delete so I assume the best call would be to iterate through every child and test for the matching sub child value but it would be great if there was a faster way.
Thanks for the help!
Heres what I'd like to delete
I assume testing for epoch time then removing the whole node would be ideal. Also not sure how to do this
I understand you don't have access to the key of the node you want do delete. Is that right? Why not? If you use the "observe()" function on a FIRDatabaseQuery object, each returned object should come with a key and a value.
Having a key it is easy to remove a node, as stated in the Firebase official guides.
From the linked guide,
Delete data
The simplest way to delete data is to call removeValue on a reference
to the location of that data.
You can also delete by specifying nil as the value for another write
operation such as setValue or updateChildValues. You can use this
technique with updateChildValues to delete multiple children in a
single API call.
So, you could try:
FirebaseDatabase.Database.database().reference(withPath: "Forum").child(key).removeValue()
or
FirebaseDatabase.Database.database().reference(withPath: "Forum").child(key).setValue(nil)
If you can't get the key in any way, what you said about "iterating" through the children of the node could be done by using a query. Here's some example code, supposing you want all forum posts by Jonahelbaz:
return FirebaseDatabase.Database.database().reference(withPath: "Forum").queryOrdered(byChild: "username").queryEqual(toValue: "Jonahelbaz").observe(.value, with: { (snapshot) in
if let forumPosts = snapshot.value as? [String: [String: AnyObject]] {
for (key, _) in forumPosts {
FirebaseDatabase.Database.database().reference(withPath: "Forum").child(key).removeValue()
}
}
})
Here you create a sorted query using as reference "username" then you ask only for the forum posts where "username" are equal to Johanelbaz. You know the returned snapshot is an Array, so now you iterate through the array and use the keys for deleting the nodes.
This way of deleting isn't very good because you might get several posts with the same username and would delete them all. The ideal case would be to obtain the exact key of the forum post you want to delete.

When I try to save data to Firebase it only updates the existing data, Swift, Firebase

The way I set up my database structure was like this:
It starts with Lists then there is a child that shows the users UID then inside that there is one item.
The one item inside the UID gets updated every time I attempt to save new data. Instead of adding another item the same one just keeps changing. I was wondering how I could instead of update the same one item every time add more items.
The way that I save my data is with this line of code.
let user = FIRAuth.auth()?.currentUser
let item: String = self.ItemTextField.text!
self.ref.child("Lists").child(user!.uid).setValue(["Items": item])
More idiomatic is to store the list of items with so-called push ids:
Lists
twEymn...
-Km....: "Yoghurt"
You'd do this with:
self.ref.child("Lists").child(user!.uid).childByAutoId().setValue(item)
The childByAutoId() generates a unique, sequential ID. It's similar to an array index, but this one works reliably in multi-user environments and when users can be offline. Read this blog post about these so-called push ids.
Alternatively you can use the name of the item as the key (if the item has to be unique in the list):
Lists
twEymn...
"Yoghurt": true
In that case the code becomes:
self.ref.child("Lists").child(user!.uid).child(item).setValue(true)
One thing you'll note is that both of these approaches only deal with the newly added item, instead of the list of items as a whole. This is a general pattern you'll see when using Firebase. By isolating your modifications, your app will be more scalable without users getting into each other's way.
The problem is that you are setting a key-value pair ("Items" : item) so that each time it is updating the value for the same key. What you could do instead is ("Items" : [your array of items here]), which will update a list of items for the same key each time.
You could also fetch the current list of items, append your new item locally, and then update.

updating many users at once in parse

I'd like to update user's column which presents related posts that the user might like,
my code is like that:
let users = query.findObjects() as [PFUser]
for user in users{
let rel = user.relationForKey("posts")
rel.addObject(post, forKey: "relatedPosts")
rel.saveInBackground()
}
I really don't know why, but I tried to do that in many versions (not only by relation, also by arrays and other methods) and it always updates just one user (the one who logged in)..
how can I get this done?
You can't update user that is not currently authenticated in your app.
The way I see it you have 2 options:
1) You can set some Cloud Code to run using the master key, so it can modify users.
2) You can add a new custom column to your Parse User class that will link to another class DB that has the related posts for the user.
take a look here: Parse.com Relations Guide
I would choose #2.

Resources