OStack程序员社区-中国程序员成长平台

标题: ios - 从 CloudKit 中的记录中仅检索一个值 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 18:26
标题: ios - 从 CloudKit 中的记录中仅检索一个值

我正在学习使用 CloudKit,并最终设法检索/查询硬编码到仪表板中的数据。我正在尝试使用 CKQueryOperation 从记录中仅查询一个值,但我总是收到每条记录中的所有值。 在下面的代码中,我试图通过使用 desiredKey 属性来获取“年龄”值 - 我得到的是“年龄”、“名字”、“姓氏”等。

let container = CKContainer.defaultContainer()
let publicData = container.publicCloudDatabase

// select all records
let predicate = NSPredicate(format: "TRUEPREDICATE", argumentArray: nil)
let query = CKQuery(recordType: "quizRecord", predicate: predicate)

// get just one value only
let operation = CKQueryOperation(query: query)
operation.desiredKeys = ["age"]


   // addOperation
    publicData.addOperation(operation)

publicData.performQuery(query, inZoneWithID: nil) { results, error in
    if error == nil { // There is no error
        // PROBLEM .. this prints out all values ????
        print(results)
        dispatch_async(dispatch_get_main_queue(), { () -> Void in
        })
    }
    else {
        print(error)
    }
}

////以下/修改后的代码可以正常工作

    let container = CKContainer.defaultContainer()
    let publicData = container.publicCloudDatabase

    // select all records
    let predicate = NSPredicate(format: "TRUEPREDICATE", argumentArray: nil)
    let query = CKQuery(recordType: "quizRecord", predicate: predicate)

    // get just one value only
    let operation = CKQueryOperation(query: query)
    operation.desiredKeys = ["age"]


    // get query
    operation.recordFetchedBlock = { (record : CKRecord) in
        // process record

        print(record)

    }


    // operation completed
    operation.queryCompletionBlock = {(cursor, error) in
        dispatch_async(dispatch_get_main_queue()) {
            if error == nil {

                print("no errors")
                // code here

            } else {

                print("error description = \(error?.description)")
            }
        }
    }

        // addOperation
    publicData.addOperation(operation)



Best Answer-推荐答案


问题很简单。您在 CKQueryOperation 上设置了 desiredKeys,但实际上从未使用过该操作。您改为在数据库上使用 performQuery

如果要使用操作并将数据限制到特定键,则需要将操作添加到数据库中(使用addOperation:)而不是调用performQuery .当然,您需要将 block 分配给操作的一个或多个 recordFetchedBlockqueryCompletionBlock 属性。

let container = CKContainer.defaultContainer()
let publicData = container.publicCloudDatabase

// select all records
let predicate = NSPredicate(format: "TRUEPREDICATE", argumentArray: nil)
let query = CKQuery(recordType: "quizRecord", predicate: predicate)

// get just one value only
let operation = CKQueryOperation(query: query)
operation.desiredKeys = ["age"]

operation.recordFetchBlock = { (record : CKRecord) in
    // process record
}
operation.queryCompletionBlock = { (cursor : CKQueryCursor!, error : NSError!) in
    // Query finished
}

// addOperation
publicData.addOperation(operation)

关于ios - 从 CloudKit 中的记录中仅检索一个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35176467/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4