NSBatchUpdateRequest:.Update 未在 didChangeObject 中触发

NSBatchUpdateRequest: .Update not firing in didChangeObject

我正在构建博客 reader,同时学习 Swift。在这里,我将数据保存到 Core Data 并可以选择将文章标记为 "read"。当我使用 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 上有关批量更新的视频。基本上更新是在磁盘上进行的,但您的内存副本数据 NOT 已更新。您必须自己更新内存中的对象,重置上下文或单独刷新每个对象。