gpt4 book ai didi

ios - 如何获取当前用户创建的所有 CloudKit 记录?

转载 作者:搜寻专家 更新时间:2023-10-30 21:55:16 27 4
gpt4 key购买 nike

假设我有一个“物种”记录类型,其中包含由许多用户创建的公共(public)记录。目前,我的查询检索“物种”的所有记录:

private func fetchSpecies() {
// Fetch Public Database
let publicDatabase = CKContainer.defaultContainer().publicCloudDatabase

// Initialize Query
let query = CKQuery(recordType: "Species", predicate: NSPredicate(format: "TRUEPREDICATE"))

// Configure Query
query.sortDescriptors = [NSSortDescriptor(key: "latinName", ascending: true)]

// Perform Query
publicDatabase.performQuery(query, inZoneWithID: nil) { (records, error) -> Void in
dispatch_async(dispatch_get_main_queue(), { () -> Void in
// Process Response on Main Thread
self.processResponseForQuery(records, error: error)
})
}
}

我如何才能只获取当前用户(如设备所有者)创建的记录?

谢谢!

最佳答案

我还在 Apple 开发者论坛上发帖,我得到了一个 excellent solution :

To fetch only a specific user records, you need to create a CKReference pointing to that user id (recordID), then use a predicate on that. It looks like this:

First fetch the user record id

yourContainer.fetchUserRecordIDWithCompletionHandler { (userID, error) -> Void in  
if let userID = userID {
// here's your userID (recordID) to play with
}
}

Then construct the predicate:

let reference = CKReference(recordID: userID, action: .None)  
let predicate = NSPredicate(format: "creatorUserRecordID == %@", reference)
let query = CKQuery(recordType: "Species", predicate: predicate)

And then use the query as normal.

取货愉快!

关于ios - 如何获取当前用户创建的所有 CloudKit 记录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35271160/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com