Thanks for having a look not sure what I am missing?
Here is what my data looks like in FireStore
This is an edit of orignal question I should have shown the model for more context. The problem was the date type.
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(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,
"createdAt" : comment.createdAt
]
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")
}
}
}
}
I can successfully add a comment with this method
func addComment(id: String, comment: Comment) {
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.arrayUnion([commentData])
])
}