如何解决CloudKit 没有以正确的顺序保存数据
我正在尝试查找有关用于 Swift 开发的 CloudKit 的一些信息,但没有找到我正在搜索的信息......这就是我请求您的帮助的原因 :)
我是 CloudKit 的新用户,我按照互联网上的一些教程学习如何处理。 现在我已经实现了保存数据并将其加载到 tableview 的可能性。 我有 3 个数据要保存,现在 TB 视图中加载了 2 个数据。到这里为止,我没有任何问题地做到了。
但我现在的大问题是,当我保存数据时,我的 cloudKit 仪表板中的数据保存顺序不正确..
例如我有:
NAME----TIMMING----TYPE
Test1---20:40-------1
Test3---21:45-------0
Test2---20:55-------2
好吧,在 CloudKit 仪表板中,这不是问题,但是当我在 TB 视图中加载信息时,我有相同的顺序。
我想要的结果是在顶行获得一个最后保存日期的 tableView...我现在不知道我该怎么做...
我的保存代码:
let privateDatabase = CKContainer.default().privateCloudDatabase
let zone = CKRecordZone(zoneName: "MealZone")
@IBAction func addMealWasPressed(_ sender: UIButton) {
let name = TitleMealTF.text as CKRecordValue?
let date = MealTimming.date as CKRecordValue
let MealNum = MealNumber.selectedSegmentIndex as CKRecordValue?
let record = CKRecord(recordType: "Meal",zoneID: zone.zoneID)
record.setObject(name,forKey: "Name")
record.setObject(date,forKey: "Timming")
record.setObject(MealNum,forKey: "Type")
let operation = CKModifyRecordsOperation(recordsToSave: [record],recordIDsToDelete: nil)
let configuration = CKOperation.Configuration()
configuration.timeoutIntervalForRequest = 10
configuration.timeoutIntervalForResource = 10
operation.configuration = configuration
operation.modifyRecordsCompletionBlock = { (savedRecords,deletedRecordIDs,error) in
DispatchQueue.main.async {
if let error = error {
print(error)
//Show an alert for user here !
}else{
print("Record was saved")
}
}
}
privateDatabase.add(operation)
self.dismiss(animated: true,completion: nil)
}
我的加载函数适用于 viewWillAppear
拥有 TB 视图的视图:
let privateDatabase = CKContainer.default().privateCloudDatabase
let zone = CKRecordZone(zoneName: "MealZone")
func queryMeal(){
let query = CKQuery(recordType: "Meal",predicate: NSPredicate(value: true))
privateDatabase.perform(query,inZoneWith: zone.zoneID) { (records,error) in
DispatchQueue.main.async {
if let error = error{
print(error)
}else{
self.records = records ?? []
self.TabViewHistory.reloadData()
}
}
}
}
最后是 TbView 代码:
var records = [CKRecord]()
func tableView(_ tableView: UITableView,numberOfRowsInSection section: Int) -> Int {
return self.records.count
}
func tableView(_ tableView: UITableView,cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "HistoryCell") as! HistoryCell
cell.backgroundColor = Beige
let record = records[indexPath.row]
cell.DescriptionLbl.text = record.object(forKey: "Name") as? String
if let date = record.object(forKey: "Timming") as? Date{
cell.TimePickLbl?.text = "Le " + dateFormatter.string(from: date)
}
return cell
}
感谢您的帮助!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。