gpt4 book ai didi

swift - NSBatchUpdateRequest : . 更新未在 didChangeObject 中触发

转载 作者:行者123 更新时间:2023-11-30 10:09:12 24 4
gpt4 key购买 nike

我正在学习 Swift 的同时构建一个博客阅读器。在此,我将数据保存到核心数据,并可以选择将文章标记为“已读”。当我使用 editActionsForRowsAtIndexPath 将它们单独标记为已读时,tableview.reloadData() 起作用并且单元格格式发生变化。

override func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]? {
let object = fetchedResultsController.objectAtIndexPath(indexPath)
let appDel: AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let context:NSManagedObjectContext = appDel.managedObjectContext
var edit = UITableViewRowAction()
if let read = object.valueForKey("read") as? Bool{
if read == true{
edit = UITableViewRowAction(style: .Normal, title: "Mark\nUnread") { action, index in
object.setValue(false, forKey: "read")
do{
try context.save()
}catch{

}
tableView.reloadData()
self.updateReadAll()
}
edit.backgroundColor = UIColor(hexaString: "#A1A19E")
}else{
edit = UITableViewRowAction(style: .Normal, title: "Mark\nRead") { action, index in
object.setValue(true, forKey: "read")
do{
try context.save()
}catch{

}
tableView.reloadData()
self.updateReadAll()
}
edit.backgroundColor = UIColor(hexaString: "#A1A19E")
}
return [edit]
}else{
return []
}
}

但是,当我使用 NSBatchUpdateRequest 时,核心数据会更新,但单元格格式不会更新。我必须强制退出应用程序并重新启动才能正确显示单元格。我在 didChangeObject 中添加了 print("Updated") 以验证它不是导致问题的 configureCell。

@IBAction func markReadAction(sender: AnyObject) {
let appDel: AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let context:NSManagedObjectContext = appDel.managedObjectContext
let table = NSBatchUpdateRequest(entityName: "BlogItems")
table.propertiesToUpdate = ["read":true]
do{
try context.executeRequest(table)
}catch{

}
updateReadAll()
tableView.reloadData()
}

func controller(controller: NSFetchedResultsController, didChangeObject anObject: AnyObject, atIndexPath indexPath: NSIndexPath?, forChangeType type: NSFetchedResultsChangeType, newIndexPath: NSIndexPath?) {
switch type {
case .Update:
self.configureCell(tableView.cellForRowAtIndexPath(indexPath!)!, atIndexPath: indexPath!)
print("Updated")
case .Insert:
tableView.insertRowsAtIndexPaths([newIndexPath!], withRowAnimation: .Fade)
case .Delete:
tableView.deleteRowsAtIndexPaths([indexPath!], withRowAnimation: .Fade)
case .Move:
tableView.deleteRowsAtIndexPaths([indexPath!], withRowAnimation: .Fade)
tableView.insertRowsAtIndexPaths([newIndexPath!], withRowAnimation: .Fade)
}
}

func configureCell(cell: UITableViewCell, atIndexPath indexPath: NSIndexPath) {
let object = self.fetchedResultsController.objectAtIndexPath(indexPath)
cell.textLabel!.text = object.valueForKey("title")!.description
cell.detailTextLabel!.text = object.valueForKey("snippet")!.description
view.backgroundColor = UIColor(hexaString: "#F8F7F5")
cell.textLabel!.textColor = UIColor.blackColor()
cell.detailTextLabel!.textColor = UIColor.blackColor()
if let read = object.valueForKey("read") as? Bool{
if read == true{
cell.textLabel!.textColor = UIColor(hexaString: "#A1A19E")
cell.detailTextLabel!.textColor = UIColor(hexaString: "#A1A19E")
}
}
}

func updateReadAll(){
if searchCoreData(NSPredicate(format: "read = %@", false)) > 0{
markReadView.enabled = true
markReadView.tintColor = UIColor.blackColor()
}else{
markReadView.enabled = false
markReadView.tintColor = UIColor(hexaString: "#A1A19E")
}
}

didChangeObject 不能用于批量更新吗?我还能做些什么来更新单元格吗?

最佳答案

观看 2014 年 WWDC 有关批量更新的视频。基本上,更新是在磁盘上进行的,但内存中的数据副本更新。您必须自己更新内存中的对象,要么重置上下文,要么单独刷新每个对象。

关于swift - NSBatchUpdateRequest : . 更新未在 didChangeObject 中触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34012007/

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