Home > Mobile >  How to write an Enum in Firestore written in SwiftUI?
How to write an Enum in Firestore written in SwiftUI?

Time:05-25

I've made a little app where I have categories, and in each categories I have different products, that works well with barcoded data, but now I want to write them in Firestore and fetch them in my application.

I have 2 Struct, one for the products :

struct Product : Identifiable, Hashable {
    var id = UUID().uuidString
    var type : ProductType
    var title : String
    var subtitle : String
    var description : String = ""
    var price : String
    var productImage : String = ""
    var quantity : Int = 1
}

and one for the category type

enum ProductType : String, CaseIterable { 
    case Wearable = "Wearable"
    case Laptops = "Laptops"
    case Phones = "Phones"
    case Tablets = "Tablets"
}

This is how I wrote the Product in Firestore : Picture 1, but I do not know how to write the enum.

Also, this is how I'm getting the data from the firestore, but I have an error at:

return Product(id: d.documentID, type: d["type"] as? String ?? ""

Cannot convert value of type 'String' to expected argument type 'ProductType'

func getData() {
        FirebaseManager.shared.firestore.collection("products").getDocuments { snapshot, error in
            if error == nil {
                
                if let snapshot = snapshot {
                    
                    DispatchQueue.main.async {
                        self.products = snapshot.documents.map { d in
                            return Product(id: d.documentID, type: d["type"] as? String ?? ""
                                           , title: d["title"] as? String ?? "",
                                           subtitle: d["subtitle"] as? String ?? "",
                                           price: d["price"] as? String ?? "", productImage: d["productImage"] as? String ?? ""
                            )
                        }
                    }

                }
            }
            else {  
            }
        }
    }

This is for "Lore Ipsum comment "

func getData() {
        FirebaseManager.shared.firestore.collection("products").getDocuments { snapshot, error in
            if error == nil {
                
                if let snapshot = snapshot {
                    
                    DispatchQueue.main.async {
                        
                        
                        self.products = snapshot.documents.compactMap { document in
                            try? document.data(as: Product.self)
                        } ?? []
            
                    }
                }
            }
            else {  
            }
        }
    }

Also, this is my FirebaseManager :

class FirebaseManager : NSObject {
    let auth : Auth
    let storage : Storage
    let firestore : Firestore
    
    
    static let shared = FirebaseManager()
    
    override init() {
        self.auth = Auth.auth()
        self.storage = Storage.storage()
        self.firestore = Firestore.firestore()
    }
}

Question, how can I write that enum into firestore?

CodePudding user response:

I think it could work like this
Even if the code readability is reduced

type: ProductType(rawValue: d["type"] as? String ?? "") ?? .Wearable // default value for ProductType

  • Related