'Firestore data is not displayed in listview. Identifiable not set up correctly?

I'm trying to fetch some data from a firebase document and have it displayed in a ListView. Getting the data and decoding it into my own struct works fine (tested by letting the console print it), but it won't show up in the actual ListView. I tried to create 2 example datasets to see if there is anything wrong with the implementation of my struct and/or ListView, but those display just fine. My best guess is that the ListView isnt happy with the id I get from the firebase document.

Here is how I fetch the data(pls ignore the ugly solution in checkAvailablePkgs() for now, that is going to change):

class firebaseController : ObservableObject {
    let databaseFIRB = Firestore.firestore()
    @Published var forUserAvailablePkgs : [DLPackage] = []
   
func checkAvailablePkgs() {
        if Auth.auth().currentUser != nil {
            var allAccessiblePkgs : [String] = []
            let userUID = Auth.auth().currentUser!.uid
            databaseFIRB.collection("user-access").document(userUID).getDocument { (document, error) in
                if let document = document, document.exists {
                    let dataDescription = document.data().map(String.init(describing:)) ?? "nil"
                    let pkgArr = dataDescription.components(separatedBy: ", ")
                    for partString in pkgArr {
                        var tmpStringSlicing : String
                        tmpStringSlicing = partString.replacingOccurrences(of: "\": <null>", with: "")
                        tmpStringSlicing = tmpStringSlicing.replacingOccurrences(of: "[\\[\\]\"]", with: "", options: .regularExpression)
                        allAccessiblePkgs.append(tmpStringSlicing)
                    }
                    self.checkIndividualPkgInformation(pkgIDs: allAccessiblePkgs)
                } else {
                    print("Document does not exist")
                }
            }
        }
    }

    func checkIndividualPkgInformation(pkgIDs: [String]) {
        for id in pkgIDs {
            databaseFIRB.collection("download-pkgs").document(id).getDocument { (document, error) in
                if let document = document, document.exists {
                    let result = Result {
                        try document.data(as: DLPackage.self)
                    }
                    switch result {
                        case .success(let pkg):
                            if let pkg = pkg {
                                // A `Package` value was successfully initialized from the DocumentSnapshot.
                                print("Package: \(pkg)")
                                self.forUserAvailablePkgs.append(pkg)
                            } else {
                                // A nil value was successfully initialized from the DocumentSnapshot,
                                // or the DocumentSnapshot was nil.
                                print("Document does not exist")
                            }
                        case .failure(let error):
                            // A `City` value could not be initialized from the DocumentSnapshot.
                            print("Error decoding package: \(error)")
                        }
                } else {
                    print("Package-Document does not exist")
                }
            }
        }
    }
}

This is the struct I want to save it as:

struct DLPackage: Identifiable, Codable {
    @DocumentID var id = UUID().uuidString
    var name : String = ""
    var size : String = ""
    var contentpaths : [String] = [""]
}

And here is the ListView that refuses to Display the aquired data:

struct FIRBauthView: View {
    let firebaseCtrl: firebaseController = firebaseController()
    var body: some View {
        VStack() {
            List(firebaseCtrl.forUserAvailablePkgs) {item in
                HStack() {
                    Text(String(item.name)).font(.custom("Avenir", size: 26))
                    Spacer()
                    Text(String(item.size)).font(.custom("Avenir", size: 26))
                }
            }
            Button {
                firebaseCtrl.checkAvailablePkgs()
            } label: {
                Text("Check")
            }
        }
    }
}

I can give my firebaseControler().forUserAvailablePkgs an initial value of:

forUserAvailablePkgs.append(contentsOf: [DLPackage(name: "Example", size: "1GB", contentpaths: ["hello","something"]),DLPackage(name: "Nr2", size: "1GB", contentpaths: ["hello","something else"])])

and they will show up just fine.

This is my print from the firebase data loaded into the forUserAvailablePkgs Array:

[Light_Player.DLPackage(_id: FirebaseFirestoreSwift.DocumentID<Swift.String>(value: Optional("CDB68E0C-DCE2-4FAA-AD55-872322A55E56")), name: "Example", size: "1GB", contentpaths: ["hello", "something"]), 
Light_Player.DLPackage(_id: FirebaseFirestoreSwift.DocumentID<Swift.String>(value: Optional("DCAAF136-24FE-470A-B897-2D178AEDB3A0")), name: "Nr2", size: "1GB", contentpaths: ["hello", "something else"]), 
Light_Player.DLPackage(_id: FirebaseFirestoreSwift.DocumentID<Swift.String>(value: Optional("package2")), name: "Bewährte Klassiker", size: "3.4GB", contentpaths: ["placeholder for another path", "placeholder for a different path"]), 
Light_Player.DLPackage(_id: FirebaseFirestoreSwift.DocumentID<Swift.String>(value: Optional("package1")), name: "Basic", size: "7.3GB", contentpaths: ["placeholder for path", "placeholder for path 2"])]

I tried changing the id for the struct to:

@DocumentID var id: String?

since I found it in a tutorial online, however the code doesn't copile that way.

I also tried overwriting the ids from the firebase data with generated UUIDs after fetching the data from the database, but I still couldn't get the ListView to display it.

Any help is appreciated.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source