Swift如何从Firestore映射文档中的嵌套数据?

时间:2019-02-17 13:05:55

标签: swift google-cloud-firestore

我在Firestore上有一个文档数据结构,如下所示: pic1 pic2 因此,文档内部有2个地图对象,并且该文档内部有一个集合和另一个文档

然后我为此文档创建3个模型swift文件

任务:

struct task {
    var Name: String
    var Address: String
    var Car: CarModel
    car Price: PriceModel

    var dictionary: [String:Any] {
        return [
            "Name" : Name,
            "Address" : Address,
            "Car" : CarModel,
            "Price" : PriceModel
        ]
    }

    init?(data: [String:Any]) {
        guard let Name = dictionary["Name"] as? String,
            let Address = dictionary["Address"] as? String,
            let Car = ditionary["car"] as? CarModel,
            let Price = dictionary["price"] as? PriceModel else{
            return nil
        }
        self.Name = Name
        self.Address = Address
        self.Car = Car
        self.Price = Price
    }
}

CarModel

struct CarModel {
    var brand: String
    var model: String
    var year: String


    var dictionary: [String:Any] {
        return [
            "brand" : brand,
            "model" : model,
            "year" : year,
        ]
    }

    init?(data: [String:Any]) {
        guard let brand = dictionary["brand"] as? String,
            let model = dictionary["model"] as? String,
            let year = ditionary["year"] as? String else{
            return nil
        }
        self.brand = brand
        self.model = model
        self.year = year
    }
}

价格模型:

struct PriceModel {
    var basic: Int
    var extra: Int


    var dictionary: [String:Any] {
        return [
            "basic" : basic,
            "extra" : extra,
        ]
    }

    init?(data: [String:Any]) {
        guard let basic = dictionary["basic"] as? Int,
            let extra = ditionary["extra"] as? Int else{
            return nil
        }
        self.basic = basic
        self.extra = extra
    }
}

然后使用以下代码下载数据:

func loadDataFromFirestore(completion: @escaping (Bool) -> ()) {
        var success: Bool = false

        DispatchQueue.global(qos: .userInteractive).async {
            let downloadGroup = DispatchGroup()
            let colRef = db.collection("tasks")
            downloadGroup.enter()
            colRef.getDocuments() { (querySnapshot, error) in
                if let error = error {
                    print("Error: \(error)")
                    return
                }

                for document in querySnapshot!.documents{
                    let result = document.data()
                    print (result)
                    if let data = task(data: result){
                        print(data)
                    }
                }
                success = true

                downloadGroup.leave()
            }
            downloadGroup.wait()
            DispatchQueue.main.async {
                completion(success)
            }
        }
    }

我可以在注释CarModel和PriceModel的情况下获取数据,但是如果我取消注释这两者,它将使我的应用程序崩溃! 那么我怎样才能使map对象适应我的代码呢? 第二个问题是:如何使用这种代码将文档放入文档集合中?

0 个答案:

没有答案