CloudKit 数据排序问题
CloudKit Data Sort Issues
不确定我在这段代码中做错了什么,但是数据拉取的排序不适用于任何数据变量。我需要按名称排序,但现在它是随机排序的。
func getStates(completionQueue: DispatchQueue = .main, completionHandler: @escaping (Result<[StateList], Error>) -> Void) {
var fetchedStates: [StateList] = []
let predicate = NSPredicate(value: true)
let sort = NSSortDescriptor(key: "name", ascending: true)
let query = CKQuery(recordType: "StateList", predicate: predicate)
let queryOperation = CKQueryOperation(query: query)
query.sortDescriptors = [sort]
queryOperation.recordMatchedBlock = { (recordID, recordResult) in
if let record = try? recordResult.get() as CKRecord {
var stateList = StateList(recordID: record.recordID)
stateList.recordID = record.recordID
stateList.id = record["id"] as? String ?? ""
stateList.name = record["name"] as? String ?? ""
stateList.initial = record["initial"] as? String ?? ""
fetchedStates.append(stateList)
print(record.recordID)
}
print("recordMatchedBlock")
}
queryOperation.queryResultBlock = { result in
DispatchQueue.main.async {
switch result {
case .success:
completionHandler(.success(fetchedStates))
case .failure(let error):
completionHandler(.failure(error))
}
}
}
database.add(queryOperation)
}
你要放;
query.sortDescriptors = [sort]
之前
let queryOperation = CKQueryOperation(query: query)
像这样;
query.sortDescriptors = [sort]
let queryOperation = CKQueryOperation(query: query)
不确定我在这段代码中做错了什么,但是数据拉取的排序不适用于任何数据变量。我需要按名称排序,但现在它是随机排序的。
func getStates(completionQueue: DispatchQueue = .main, completionHandler: @escaping (Result<[StateList], Error>) -> Void) {
var fetchedStates: [StateList] = []
let predicate = NSPredicate(value: true)
let sort = NSSortDescriptor(key: "name", ascending: true)
let query = CKQuery(recordType: "StateList", predicate: predicate)
let queryOperation = CKQueryOperation(query: query)
query.sortDescriptors = [sort]
queryOperation.recordMatchedBlock = { (recordID, recordResult) in
if let record = try? recordResult.get() as CKRecord {
var stateList = StateList(recordID: record.recordID)
stateList.recordID = record.recordID
stateList.id = record["id"] as? String ?? ""
stateList.name = record["name"] as? String ?? ""
stateList.initial = record["initial"] as? String ?? ""
fetchedStates.append(stateList)
print(record.recordID)
}
print("recordMatchedBlock")
}
queryOperation.queryResultBlock = { result in
DispatchQueue.main.async {
switch result {
case .success:
completionHandler(.success(fetchedStates))
case .failure(let error):
completionHandler(.failure(error))
}
}
}
database.add(queryOperation)
}
你要放;
query.sortDescriptors = [sort]
之前
let queryOperation = CKQueryOperation(query: query)
像这样;
query.sortDescriptors = [sort]
let queryOperation = CKQueryOperation(query: query)