篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了swift Swift - CloudKit:获取记录相关的知识,希望对你有一定的参考价值。
01. Using a query with a predicate to get a list of records.
let privateDB = CKContainer.defaultContainer().privateCloudDatabase
let predicate = NSPredicate(format: "firstName = %@", "John")
let query = CKQuery(recordType: "Artist", predicate: predicate)
privateDB.performQuery(query, inZoneWithID: nil) { records, error in
guard let records = records else { return }
//Use the records..
}
02. Alternative query with a predicate to get a list of records.
var publicDatabase: CKDatabase? = CKContainer.default().publicCloudDatabase
var predicate = NSPredicate(format: "title = %@", "Santa Cruz Mountains")
var query = CKQuery(recordType: "Artwork", predicate: predicate)
publicDatabase?.perform(query, inZoneWith: nil, completionHandler: {(_ results: [Any], _ error: Error?) -> Void in
if error != nil {
// Error handling for failed fetch from public database
}
else {
// Display the fetched records
}
})
03. Fetching individual records by record ID (the identifier)
let privateDB = CKContainer.defaultContainer().privateCloudDatabase
let artistID = CKRecordID(recordName: "16EBF80D-E59B-4966-B6D5–790E6D79E4D1")
privateDB.fetchRecordWithID(artistID) { record, error in
guard error == nil else { return }
//Use the records..
}
04. Alternative, fetching individual records by record ID (the identifier)
var publicDatabase: CKDatabase? = CKContainer.default().publicCloudDatabase
var artworkRecordID = CKRecordID(recordName: "115")
publicDatabase?.fetch(withRecordID: artworkRecordID, completionHandler: {(_ artworkRecord: CKRecord, _ error: Error?) -> Void in
if error != nil {
// Error handling for failed fetch from public database
}
else {
// Display the fetched record
}
})
以上是关于swift Swift - CloudKit:获取记录的主要内容,如果未能解决你的问题,请参考以下文章