Reputation: 505
I have a tableView with data populated from Firebase, when I click a delete button the data is removed from Firebase but it remains on my app and it doesn't remove the data from the tableView until I close the app and reopen it. Here is how I set up the delete function:
func deletePost() {
let uid = FIRAuth.auth()!.currentUser!.uid
let storage = FIRStorage.storage().reference(forURL: "gs://gsignme-14416.appspot.com")
FIRDatabase.database().reference().child("posts").child(uid).observe(.childAdded, with: { (snapshot) in
let indexPath = self.selectedIndex
let post = self.posts[(indexPath?.row)!] as! [String: AnyObject]
self.key = post["postID"] as? String
self.itemsRef = FIRDatabase.database().reference().child("posts").child(uid).child(self.key!)
// Remove the post from the DB
FIRDatabase.database().reference().child("books").child(self.key!).removeValue { error in
if error != nil {
print("error \(error)")
}
}
})
self.TableView.reloadData()
}
Here are the delegate and datasource:
func numberOfSections(in tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
posts.count
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
self.selectedIndex = indexPath
self.didExpandCell()
if isExpanded && self.selectedIndex == indexPath{
print(indexPath)
} else{
}}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if isExpanded && self.selectedIndex == indexPath{
return 300
}
return 126
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cells", for: indexPath) as! ProfileTableViewCell
//Configure the cell
let post = self.posts[indexPath.row] as! [String: AnyObject]
cell.Title.text = post["title"] as? String
cell.Author.text = post["Author"] as? String
cell.ISBN10.text = post["ISBN10"] as? String
return cell
}
I attempted to add a tableview.reloaddata at the end of the function but that doesn't help. What am I doing wrong?
Upvotes: 2
Views: 2914
Reputation: 2751
Delete the removed object from your array also. Your table view gets populated using posts
array, not from Firebase object itself. when you click delete button the data is removed from Firebase but it remains on your app, as you have not deleted that object from your array i.e. posts
thats why it doesn't remove the data from the tableView until you close the app and reopen it.
You need the remove the deleted object from your posts array and reload that rows to get the effect.
self.posts.remove(at: indexPath.row)
and then reload that specific row itself.
self.tableView.beginUpdates
tableView.reloadRows(at: [indexPath], with: .top)
self.tableView.endUpdates;
In your case
FIRDatabase.database().reference().child("books").child(self.key!).
removeValue { error in
if error != nil {
print("error \(error)")
} else{
self.posts.remove(at: indexPath.row)
self.tableView.beginUpdates
tableView.reloadRows(at: [indexPath], with: .top)
self.tableView.endUpdates;
}
}
})
Hope it helps. Happy Coding!!
Upvotes: 0
Reputation: 71854
Remove your object from posts
array and then reload your tableView
in main queue one you remove your object from firebase.
Check below code:
func deletePost() {
let uid = FIRAuth.auth()!.currentUser!.uid
let storage = FIRStorage.storage().reference(forURL: "gs://gsignme-14416.appspot.com")
FIRDatabase.database().reference().child("posts").child(uid).observe(.childAdded, with: { (snapshot) in
let indexPath = self.selectedIndex
let post = self.posts[(indexPath?.row)!] as! [String: AnyObject]
self.key = post["postID"] as? String
self.itemsRef = FIRDatabase.database().reference().child("posts").child(uid).child(self.key!)
// Remove the post from the DB
FIRDatabase.database().reference().child("books").child(self.key!).removeValue { error in
if error != nil {
print("error \(error)")
} else {
//Here remove your object from table array
self.posts.remove(at: indexPath?.row)
//Reload your tableview in main queue
DispatchQueue.main.async{
self.TableView.reloadData()
}
}
}
})
}
Didn't tested it so let me know if you still have issue with above code.
Upvotes: 1
Reputation: 1389
func deletePost() {
let uid = FIRAuth.auth()!.currentUser!.uid
let storage = FIRStorage.storage().reference(forURL: "gs://gsignme-14416.appspot.com")
FIRDatabase.database().reference().child("posts").child(uid).observe(.childAdded, with: { (snapshot) in
let indexPath = self.selectedIndex
let post = self.posts[(indexPath?.row)!] as! [String: AnyObject]
self.key = post["postID"] as? String
self.itemsRef = FIRDatabase.database().reference().child("posts").child(uid).child(self.key!)
// Remove the post from the DB
FIRDatabase.database().reference().child("books").child(self.key!).removeValue { error in
if error != nil {
print("error \(error)")
return // use return here or use if else
}
// no error has occurred,hence move on to remove the post from the array
self.posts.remove(at: indexPath.row)
} })
DispatchQueue.main.async {
self.TableView.reloadData()
}
}
Upvotes: 0