gpt4 book ai didi

swift - 如何重新加载经过编辑的更改的 CloudKit 单元

转载 作者:行者123 更新时间:2023-11-30 11:06:22 28 4
gpt4 key购买 nike

我正在使用 CloudKit 作为数据库创建一个简单的待办事项列表应用程序。目前我可以添加和删除对象,但对编辑对象感到困惑。

编辑项目 Controller

protocol EditItemControllerDelegate {
func editItemController(_ controller: EditItemController, didFinishEditing item: ItemCK)
}

class EditItemController: UIViewController {

var delegate: EditItemControllerDelegate?
var itemToEdit: ItemCK?

let itemTextField: UITextField = {
let textField = UITextField()
textField.placeholder = "Hello"
return textField
}()

@objc func handleSave() {

if let item = itemToEdit {
guard let itemName = self.itemTextField.text else { return }
item.name = itemName
delegate?.editItemController(self, didFinishEditing: item)
}
}

项目 Controller

class ItemController: UITableViewController, UITextFieldDelegate, EditItemControllerDelegate {

func editItemController(_ controller: EditItemController, didFinishEditing item: ItemCK) {

if let index = itemsModel.items.index(of: item) {
let indexPath = IndexPath(row: index, section: 0)
if let cell = tableView.cellForRow(at: indexPath) as? ItemCell {
cell.itemLabel.text = item.name
print(item.name)
}
}
}

因此,当我编辑对象时, TableView 单元格会重新加载新的对象数据。但是当我向后滑动时,它会返回到原始对象。它似乎没有存储到数据库中。如何编辑对象、存储它,然后将其呈现到表格 View 单元格中?

What app currently look like

CloudKit 代码

class ItemCK: NSObject {
static let recordType = "Item"
static let keys = (name: "title")

var record: CKRecord
init(record: CKRecord) {
self.record = record
}

override init() {
record = CKRecord(recordType: ItemCK.recordType)
}

var name: String {
get {
return record.value(forKey: ItemCK.keys.name) as! String
}
set {
record.setValue(newValue, forKey: ItemCK.keys.name)
}
}
}


class ItemModel {
private let database = CKContainer.default().privateCloudDatabase

var items = [ItemCK]() {
didSet {
self.notificationQueue.addOperation {
self.onChange?()
}
}
}

var onChange: (() -> Void)?
var onError : ((Error) -> Void)?
var notificationQueue = OperationQueue.main

var records = [CKRecord]()
var insertedObjects = [ItemCK]()
var editedObjects = ItemCK()
//to keep track of deleted objects
var deletedCKObjectIds = Set<CKRecord.ID>()

init() {
}

func addCategory(name: String) {
print("Adding item")
let item = ItemCK()
item.name = name
database.save(item.record) { (_, error) in
guard error == nil else {
self.handle(error: error!)
return
}
}

insertedObjects.append(item)
updateItem()
}

func editItem() {
let privateDatabase = database
var predicate= NSPredicate
}

func delete(at index: Int) {
print("Deleting item")
let recordId = self.items[index].record.recordID
database.delete(withRecordID: recordId) { (_, error) in
guard error == nil else {
self.handle(error: error!)
return
}
}

deletedCKObjectIds.insert(recordId)
updateItem()
}

@objc func refresh() {
print("refreshing")
let query = CKQuery(recordType: ItemCK.recordType, predicate: NSPredicate(value: true))
database.perform(query, inZoneWith: nil) { (records, error) in
guard let records = records, error == nil else {
self.handle(error: error!)
return
}
self.records = records
self.updateItem()
}
}

private func updateItem() {

print("Updating item")
var knownIds = Set(records.map {$0.recordID})

self.insertedObjects.removeAll { (item) -> Bool in
knownIds.contains(item.record.recordID)
}

knownIds.formUnion(insertedObjects.map {$0.record.recordID})

self.deletedCKObjectIds.formIntersection(knownIds)
var items = records.map { (record) in ItemCK(record: record) }

items.append(contentsOf: insertedObjects)
items.removeAll { (items) -> Bool in
deletedCKObjectIds.contains(items.record.recordID)
}

self.items = items

}
}

抱歉源代码太长。 CloudKit 太令人困惑了,不知道哪些信息值得分享或不重要

最佳答案

database.save 在 updateItem() 函数中。

当您使用新的 RecordID 保存记录时,它将插入该记录。

当您使用与现有记录匹配的 RecordID 保存记录时,它将覆盖该记录(取决于您的冲突解决设置)。

关于swift - 如何重新加载经过编辑的更改的 CloudKit 单元,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52669489/

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