Reputation: 23
I have a UICollectionView which looks like this image and have the following data structure in Firebase.
I would like for the user to be able to delete individual posts from the collection view and subsequently from firebase. I have seen on other stackoverflow posts that say I must use the .removeValue from firebase, but don't know how to get the reference to the random child in order to delete it.
How can I access the autoId value from each post e.g "LPmNrvzu-aXsw_u-rEF " so I can remove that child node from Firebase?
Here's the path I was using to load all the user's posts from Firebase:
@objc func observeUserPosts() {
let uid = Auth.auth().currentUser?.uid
let postsRef = Database.database().reference().child("posts").queryOrdered(byChild: "author/userid")
postsRef.queryEqual(toValue: uid!).observe(.value) { (snapshot) in
}
}
This is the extension where I'm loading all the UICollectionView code
//extension - UICollectionView for user's posts
extension ProfileViewController: UICollectionViewDataSource,UICollectionViewDelegate {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return postsuser.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell: PostsCollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "postsCell", for: indexPath) as! PostsCollectionViewCell
cell.set(post: postsuser[indexPath.row])
cell.deletePostButton.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
cell.deletePostButton.tag = indexPath.row
return cell
}
@objc func buttonAction(sender: UIButton) {
Database.database().reference().child("posts").queryOrdered(byChild: "author/userid").observe(.value) { (snapshot) in
if let posts = snapshot.value as? [String: AnyObject] {
for (key, _) in posts {
// NOW HOW DO I REFERENCE THE CELL THAT THE USER CLICKS TO DELETE?
}
}
}
// postsuser[sender.tag]
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let vc = storyboard?.instantiateViewController(withIdentifier: "profileUsersSelectedPostViewController") as? ProfileUsersSelectedPostViewController
self.navigationController?.pushViewController(vc!, animated: true)
vc?.selectedpostsuser = postsuser[indexPath.row]
}
}
Upvotes: 1
Views: 416
Reputation: 23
This is how I managed to solve the question I had asked.... hope it helps :)
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell: PostsCollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "postsCell", for: indexPath) as! PostsCollectionViewCell
cell.set(post: postsuser[indexPath.row])
cell.deletePostButton.addTarget(self, action: #selector(buttonAction(sender:)), for: .touchUpInside)
cell.deletePostButton.tag = indexPath.row
return cell
}
@objc func buttonAction(sender: UIButton) {
ProgressHUD.show("Un momento", interaction: true)
let uid = Auth.auth().currentUser?.uid
Database.database().reference().child("posts").queryOrdered(byChild: "author/userid").queryEqual(toValue: uid!).observe(.value) { (snapshot) in
if let posts = snapshot.value as? [String: AnyObject] {
if let posts = snapshot.value as? [String: AnyObject] {
for (key, postReference) in posts {
if let post = postReference as? [String: Any], let timestamp = post["timestamp"] as? TimeInterval, timestamp == self.postsuser[sender.tag].timestampDouble {
Database.database().reference().child("posts").child(key).removeValue(completionBlock: { (error, _) in
DispatchQueue.main.async {
ProgressHUD.showSuccess("Tu imagen ha sido borrada...")
self.postsuser.remove(at: sender.tag)
self.postsCollectionView.reloadData()
self.refresher.endRefreshing()
}
})
}
}
}
}
}
}
Upvotes: 1