Hi,
I have an app that is a shopping list. I can store prices per product and vendor in my app, the model is
Product
Vendor
Price
One product can have multiple prices from different vendors.
I store the price information with references to the product and vendor (CKRecord.Reference).
Now I am using the below to fetch all the prices related to a product:
My problem is now that I cannot retrieve the vendor name which is part of the Vendor object (Vendor.name).
I have tried to loop over the pricesBuffer and run this one per price but the problem seems to be that CloudKit first completes the initial request to fetchDataByProduct() and then afterwards fetches the vendor data but then its too late because that updated data does not get pushed to my View (SwiftUI).
Any ideas how to solve this? I believe I have to add a second CKQueryOperation to the mix and use the .addDependency() but I cannot wrap my head around how that should look like in the end.
Max
I have an app that is a shopping list. I can store prices per product and vendor in my app, the model is
Product
Vendor
Price
One product can have multiple prices from different vendors.
I store the price information with references to the product and vendor (CKRecord.Reference).
Now I am using the below to fetch all the prices related to a product:
Code Block public func fetchDataByProduct(product: Product, completionHandler: @escaping (Bool) -> Void){ self.pricesBuffer = [] let cloudContainer = CKContainer.init(identifier: "iCloud.XYZ") let publicDatabase = cloudContainer.publicCloudDatabase let reference = CKRecord.Reference(recordID: product.recordID, action: .deleteSelf) let predicate = NSPredicate(format: "priceToProduct == %@", reference) let query = CKQuery(recordType: "Price", predicate: predicate) let operation = CKQueryOperation(query: query) operation.recordFetchedBlock = { record in let price = Price() price.recordID = record.recordID price.grossPrice = record.object(forKey: "grossPrice") as? Double let dummy = record.object(forKey: "priceToVendor") as! CKRecord.Reference price.vendorRecordID = dummy.recordID self.pricesBuffer.append(price) } operation.queryCompletionBlock = { [unowned self] (cursor, error) in self.pricesBuffer.forEach({price in price.retrieveVendor() }) DispatchQueue.main.async { if error == nil { self.prices = self.pricesBuffer completionHandler(true) } else { } } } publicDatabase.add(operation) }
My problem is now that I cannot retrieve the vendor name which is part of the Vendor object (Vendor.name).
I have tried to loop over the pricesBuffer and run this one per price but the problem seems to be that CloudKit first completes the initial request to fetchDataByProduct() and then afterwards fetches the vendor data but then its too late because that updated data does not get pushed to my View (SwiftUI).
Code Block publicDatabase.fetch(withRecordID: self.vendorRecordID, completionHandler: {[unowned self] record, error in if let record = record { print(record) self.vendor.recordID = record.recordID self.vendor.name = record["name"] as! String print(self.vendor.name) } })
Any ideas how to solve this? I believe I have to add a second CKQueryOperation to the mix and use the .addDependency() but I cannot wrap my head around how that should look like in the end.
Max