gpt4 book ai didi

ios - ManagedContext.save 后核心数据未更新,但未引发错误

转载 作者:行者123 更新时间:2023-11-29 05:50:32 25 4
gpt4 key购买 nike

我正在尝试更新核心数据中已保存的条目,objectContext 不为零,管理对象也不为零。新值将设置为具有所述 Id 的托管对象。调用 ManagedContext.save() 时不会引发任何错误,但 coreData 中不会反射(reflect)任何更改

我尝试使用 setValue() 无济于事,我在谷歌和堆栈溢出上广泛搜索了类似的问题,大多数时候它说 ManagedObject 为零。

    var taskName:String!
var resultsController: NSFetchedResultsController<Tasks>!

// MARK: - Properties
// Manage objects and update tasks
//var resultsController: NSFetchedResultsController<Tasks>!
var managedContext: NSManagedObjectContext!
var tasksArray = [NSManagedObject]()
var fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "Tasks")


func settextfields() {
let res = resultsController.fetchedObjects!
print(res)
for r in res {
if r.name == taskName {
txt_name.text = "\(r.name ?? "Task name" )"
txt_date.text = "\(r.date ?? "Task date")"
segmentedBtn.selectedSegmentIndex = Int(r.priority)
}
}
}

func loadTable() {
let request: NSFetchRequest<Tasks> = Tasks.fetchRequest()
// Sort by date
let sortDescriptor = NSSortDescriptor(key: "date", ascending: true)
request.sortDescriptors = [sortDescriptor]
resultsController = NSFetchedResultsController(fetchRequest: request, managedObjectContext: coreData.managedContext, sectionNameKeyPath: nil, cacheName: nil)

// Fetch data
do {
try resultsController.performFetch()
print("Fetch successful")
} catch {
print("Error performing fetch: \(error)")
}
}

@IBAction func saveEdit(_ sender: Any) {
guard let name = txt_name.text, !name.isEmpty else {
return
}

guard let date = txt_date.text, !date.isEmpty else {
return
}


do {
loadTable()
let res = resultsController.fetchedObjects!
for r in res {
i += 1
if r.name == taskName {
print(r)
guard let name = txt_name.text, !name.isEmpty else {
return
}
guard let date = txt_date.text, !date.isEmpty else {
return
}
do {
r.name = name
r.date = date
r.priority = Int16(segmentedBtn.selectedSegmentIndex)
}

do {
try managedContext.save()
print(managedContext, r)
dismiss(animated: true)
print("Edit Successful!")
} catch {
print("Error saving task: \(error)")
}
}
}

}

最佳答案

我实际上使用谓词重写了保存函数,就像上面的人建议的那样,并且它有效。然后我向 mainViewController 添加了一个 viewWillLoad,以便在条目更新后重新加载 tableView

@IBAction func saveEdit(_ sender: Any) {
guard let name = txt_name.text, !name.isEmpty else {
return
}

guard let date = txt_date.text, !date.isEmpty else {
return
}


do {
let fetchRequest:NSFetchRequest<NSFetchRequestResult> = NSFetchRequest.init(entityName: "Tasks")
fetchRequest.predicate = NSPredicate(format: "name = %@", taskName)
let test = try managedContext.fetch(fetchRequest)
let obj = test[0] as! NSManagedObject

obj.setValue(name, forKey: "name")
obj.setValue(date, forKey: "date")
obj.setValue(Int16(segmentedBtn.selectedSegmentIndex), forKey: "priority")


do {
try managedContext.save()
dismiss(animated: true)
print("Edit Successful!")
} catch {
print("Error saving task: \(error)")
}
} catch {
print(error)
}
}

关于ios - ManagedContext.save 后核心数据未更新,但未引发错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55719038/

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