启动应用程序时,我正在使用 CloudKit 下载文件。我有一个 CKQuery 可以找到所有尚未下载的文件。当只有一两个要下载时它可以正常工作,但是当下载的数量很大时,它会失败并出现错误:
\"服务器拒绝请求\" (15/2000); server message = \"内部服务器错误\";
这发生在设备或模拟器上。
相关代码
let container = CKContainer(identifier: "iCloud.quaritate.Agni")
let publicDB = container.publicCloudDatabase
let privateDB = container.privateCloudDatabase
let predicate = NSPredicate(format: "NOT (%@ CONTAINS Name)", downloadedTitles)
let query = CKQuery(recordType: "List", predicate: predicate)
publicDB.performQuery(query, inZoneWithID: nil, completionHandler: {
results, error in
if error != nil{
//error getting lists
NSLog("\(error?.description)")
}else{
//got lists
}
}
编辑
我已经更新了我的代码以使用 CKOperationQueue
func getNewWords(){
let predicate = NSPredicate(format: "NOT (%@ CONTAINS Name)", downloadedTitles)
let query = CKQuery(recordType: "List", predicate: predicate)
let queryOperation = CKQueryOperation(query: query)
let operationQueue = NSOperationQueue()
self.executeQueryOperation(queryOperation, onOperationQueue: operationQueue)
}
func executeQueryOperation(queryOperation: CKQueryOperation, onOperationQueue operationQueue: NSOperationQueue) {
NSLog("Executing query operation")
let container = CKContainer(identifier: "iCloud.quaritate.Agni")
let publicDB = container.publicCloudDatabase
queryOperation.database = publicDB
queryOperation.recordFetchedBlock = {(record:CKRecord) in
self.saveRecord(record)
}
queryOperation.queryCompletionBlock = {(cursor:CKQueryCursor?, error:NSError?)->Void in
guard error == nil else{
NSLog("Query error: \(error?.description)")
return
}
if let queryCursor = cursor { //there are more operations to execute
let queryCursorOperation = CKQueryOperation(cursor: queryCursor)
self.executeQueryOperation(queryCursorOperation, onOperationQueue: operationQueue)
}
}
operationQueue.addOperation(queryOperation)
}
但仍然收到相同的错误。