我正在尝试使用CloudKit从iCloud检索数据。因为我只想这样做一次,所以我将代码放在AppDelegate.swift文件的didFinishLaunchingWithOptions中。
我尝试在主线程上运行它,这样直到我获得数据之前,我的第一个ViewController中的代码都不会被调用。
func getPeople() {
let query = CKQuery(recordType: "Person", predicate: Predicate(format: "TRUEPREDICATE", argumentArray: nil))
CKContainer.default().privateCloudDatabase.perform(query, inZoneWith: nil) { (records, error) in
if error == nil {
names.removeAll()
messages.removeAll()
pictures.removeAll()
recordIds.removeAll()
for record in records! {
names.append(record.object(forKey: "name") as! String)
messages.append(record.object(forKey: "message") as! String)
pictures.append(UIImage(data: record.object(forKey: "picture") as! NSData as Data)!)
recordIds.append(record.recordID)
}
print("done")
} else {
print(error)
print(error?.code)
}
}
}
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
performSelector(onMainThread: #selector(getPeople), with: nil, waitUntilDone: true)
return true
}然而,这似乎不起作用。在我的ViewController中,在viewDidLoad之后调用print("done")。我以前没有使用过多线程,我想知道如何阻止其他代码被调用,直到我的查询完成。谢谢!
https://stackoverflow.com/questions/38127550
复制相似问题