Reputation: 171
Thanks for having a look not sure what I am missing?
Here is what my data looks like in Firestore
Here is Comment Model. Note I should have shown this in original question for more context.
import SwiftUI
import FirebaseFirestoreSwift
struct Comment: Identifiable, Codable, Hashable {
var id: String
var userId: String
var storyId: String
var commentText: String
var createdAt: Date
}
Here is method where I am deleting Firestore data. No error is being thrown? The print statements in method print the expected firestore document id and the whole comment object in question But it also prints the success case and nothing changes in UI or FireStore.
func deleteComment(id: String, comment: Comment) {
print(id, comment)
//prints firestore document id and whole comment object
let commentData: [String: Any] = [
"id" : comment.id as Any,
"userId" : comment.userId,
"storyId" : comment.storyId,
"commentText" : comment.commentText,
"createdAt" : comment.createdAt
]
store.collection(path).document(id).updateData([
"comments" : FieldValue.arrayRemove([commentData])
]) { error in
if let error = error {
print("Unable to delete comment: \(error.localizedDescription)")
} else {
print("Successfully deleted comment")
}
}
}
Upvotes: 0
Views: 300
Reputation: 171
It turns out it was a type thing the createdAt property in my model is of type date. I edited my original question to show model for more context
So I for time being I just removed the createdAt property Thanks to @jnpdx for all the help.
func deleteComment(docId: String,comment: Comment) {
print("ID here",docId)
print("Comment",comment)
let commentData: [String: Any] = [
"id" : comment.id,
"userId" : comment.userId,
"storyId" : comment.storyId,
"commentText" : comment.commentText
]
DispatchQueue.main.async {
self.store.collection(self.path).document(docId).updateData([
"comments" : FieldValue.arrayRemove([commentData])
]) { error in
if let error = error {
print("Unable to delete comment: \(error.localizedDescription)")
} else {
print("Successfully deleted comment")
}
}
}
}
Upvotes: 1
Reputation: 68
You are only removing the id
from the comments
section. You should remove the whole data as you do when you add a comment.
func deleteComment(id: String, comment: Comment) {
print(id, comment.id)
// print results QDobPXyOM2WSLUA0j7Cj
//55BAE423-B32F-41E3-8B2CB594BF9002D7
let commentData: [String: Any] = [
"id" : comment.id as Any,
"userId" : comment.userId,
"storyId" : comment.storyId,
"commentText" : comment.commentText,
"createdAt" : comment.createdAt
]
let docRef = store.collection(path).document(id)
docRef.updateData([
"comments" : FieldValue.arrayRemove([commentData])
])
}
Upvotes: 0