UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: SwiftUI Firestore, trouble deleting an element in an array nested in Firestore document.

Forums > SwiftUI

Thanks for having a look not sure what I am missing?

Here is what my data looks like in FireStore Firestore Data

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])
        ])
    }

3      

I finally figured out what I was doing wrong.

My Comment model had a property, createdAt : Date

It turns out it was just a type thing.

3      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.