我正在使用SwiftUI制作应用程序并将数据存储在ICloud上。因为我可以找到的所有代码都与Swift和viewDidLoad和TableView有关,但是这并不适用。我已经编写了代码,似乎可以检索它,但不会将其返回给ObservableObject以便能够在SwiftUI中显示]
ObservableObject文件:-
import SwiftUI import Combine final class ObservedData: ObservableObject { @Published var venues = venuesData }
检索数据的查询
import SwiftUI import CloudKit var venuesData: [Venue] = loadVenues() func loadVenues() -> [Venue] { var data = [Venue]() let pred = NSPredicate(value: true) let sort = NSSortDescriptor(key: "id", ascending: true) let query = CKQuery(recordType: "DeptfordHopData", predicate: pred) query.sortDescriptors = [sort] let operation = CKQueryOperation(query: query) operation.desiredKeys = ["id","name", "address"] operation.resultsLimit = 50 var newVenues = [Venue]() operation.recordFetchedBlock = { record in let venue = Venue() venue.racordID = record.recordID venue.id = record["id"] venue.name = record["name"] venue.address = record["address"] newVenues.append(venue) } operation.queryCompletionBlock = {(cursor, error) in DispatchQueue.main.async { if error == nil { data = newVenues } else { print(error!.localizedDescription) } } } CKContainer.default().publicCloudDatabase.add(operation) return data }
我有数据显示何时中断数据但未传递给会场数据
我正在使用SwiftUI制作应用程序并将数据存储在ICloud上。因为我可以找到的所有代码都与Swift和viewDidLoad和TableView有关,但是这并不适用。我已经编写了代码,并且...
查询操作异步运行。当您将操作添加到数据库时,框架将在后台运行查询,并立即将控制权返回给您的函数,该函数会立即返回data
,该值仍设置为空数组。