简体   繁体   English

CKQueryOperation queryCompletionBlock 返回零 cursor

[英]CKQueryOperation queryCompletionBlock return a nil cursor

I'm fetching a CloudKit database with CKQueryOperation.我正在使用 CKQueryOperation 获取 CloudKit 数据库。 For some reason every time when I press a fetch button, the first time I get a nil cursor.出于某种原因,每次按下获取按钮时,我第一次得到一个零 cursor。 The second time it fetches and gets data, it's all good.第二次获取和获取数据时,一切都很好。 When I check the recordFetchedBlock it does get the results and appends them, but at the end the array is empty.当我检查recordFetchedBlock时,它确实得到了结果并附加了它们,但最后数组是空的。 I don't understand why this happens.我不明白为什么会这样。 I want to show the results immediately since they have been fetched.我想立即显示结果,因为它们已被提取。 I think the problem is with the nil cursor, but I'm open for other suggestions.我认为问题出在零 cursor 上,但我愿意接受其他建议。 Here's my code:这是我的代码:

public class CloudKitDatabase {

static let shared = CloudKitDatabase()

var records = [CKRecord]()

let publicData = CKContainer.default().publicCloudDatabase

init() {
    self.fetchRecords()
}

func fetchRecords() {

    let predicate = NSPredicate(value: true)
    let query = CKQuery(recordType: "OECD", predicate: predicate)

    let queryOperation = CKQueryOperation(query: query)

    queryOperation.recordFetchedBlock = {
        record in
        self.records.append(record)


    }

    queryOperation.queryCompletionBlock = { cursor, error in
        DispatchQueue.main.async {
            if error != nil {
                print(error.debugDescription)
            } else {
                if cursor != nil {
                    self.queryServer(cursor!)
                } else {
                    print("CURSOR IS NIL")
                }
            }
        }

    }
    self.publicData.add(queryOperation)
}


func queryServer(_ cursor: CKQueryOperation.Cursor) {
    let queryOperation = CKQueryOperation(cursor: cursor)

    queryOperation.recordFetchedBlock = {
        record in
        self.records.append(record)

    }

    queryOperation.queryCompletionBlock = { cursor, error in
        DispatchQueue.main.async {
            if error != nil {
                print(error.debugDescription)
            } else {
                if cursor != nil {
                    self.queryServer(cursor!)
                } else {
                    print("CURSOR IS NIL")
                }
            }
        }
    }
    self.publicData.add(queryOperation)

}

The Debug area tells me that: CURSOR IS NIL and CloudKitDatabase.shared.records.isEmpty is true调试区域告诉我: CURSOR IS NIL 和 CloudKitDatabase.shared.records.isEmpty 是真的

First try some configs on the first query;首先在第一个查询中尝试一些配置;

   let queryOperation = CKQueryOperation(query: query)
   queryOperation.queuePriority = .veryHigh
   queryOperation.resultsLimit = 99 // built in limit is 400  

Next, don't do the cursor calls in a dispatch and include your completions;接下来,不要在调度中执行 cursor 调用并包含您的完成;

queryOperation.queryCompletionBlock =
{ cursor, error in

        if error != nil {
            print(error.debugDescription)
        } else {
            if cursor != nil {
                self.queryServer(cursor!)
            } else {
                print("CURSOR IS NIL")
                completion(nil)
            }
        }
}

and;和;

queryOperation.queryCompletionBlock = 
{ cursor, error in

        if error != nil {
            print(error.debugDescription)
        } else {
            if cursor != nil {
                self.queryServer(cursor!)
            } else {
                print("CURSOR IS NIL")
                completion(nil)
            }
        }

}

also don't forget to empty your records array at the beginning of fetchRecords otherwise successive calls will get the same records in the array.也不要忘记在 fetchRecords 开始时清空您的记录数组,否则连续调用将在数组中获得相同的记录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM