Home > Software design >  documentReference.data(as: ) not available anymore in Firestore?
documentReference.data(as: ) not available anymore in Firestore?

Time:01-21

I'm trying to access the documentReference.data(as: ) but it seems like it wouldn't show on my end. I was wondering if this method is deprecated? if so is there any alternative method I can use to map a document reference to a Swift type.

Here's a screenshot of my code:

documentReference.data(as: ) not showing

What I'm try to do is something like this.

func fetchOccupants() async throws -> [Occupant] {
    let snapshot = try await occupantsRef.getDocuments()
    
    
    return snapshot.documents.compactMap { document in
        try? document.data(as: Occupants.self)
    }
}

CodePudding user response:

I figured it out. Apparently, at some point this method was moved to a separate module FirebaseFirestoreSwift (or maybe it was always there, I might be wrong).

Import this module and the method data(as:with:decoder:) becomes available:

import FirebaseFirestoreSwift

It's also mentioned on the documentation page about Codable: Map Cloud Firestore data with Swift Codable.

CodePudding user response:

After a few days of testing and reading some documentation I finally solved it. It seems like using Cocoapods to add FirebaseFirestoreSwift is not working, so I tried using Swift Package Manager instead since it's more updated Package Manager for Swift.

Following this method works for me link

  • Related