我遇到了 Xcode 报告的严重问题:
<强>1。我有一个 NSFetchResultsController
var fetchedResultsController: NSFetchedResultsController {
if _fetchedResultsController != nil {
return _fetchedResultsController!
}
let fetchRequest = NSFetchRequest()
// Edit the entity name as appropriate.
let entity = NSEntityDescription.entityForName("Choice", inManagedObjectContext: NSManagedObjectContext.MR_defaultContext())
fetchRequest.entity = entity
// Set the batch size to a suitable number.
fetchRequest.fetchBatchSize = 10
// Edit the sort key as appropriate.
let sortDescriptor = NSSortDescriptor(key: "id", ascending: false)
let sortDescriptors = [sortDescriptor]
fetchRequest.sortDescriptors = sortDescriptors
//NSPredicate
let predicate = NSPredicate(format: "decision.id = %@", decision.id!)
fetchRequest.predicate = predicate
// Edit the section name key path and cache name if appropriate.
// nil for section name key path means "no sections".
let aFetchedResultsController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: NSManagedObjectContext.MR_defaultContext(), sectionNameKeyPath: nil, cacheName: nil)
aFetchedResultsController.delegate = self
_fetchedResultsController = aFetchedResultsController
do{
try _fetchedResultsController?.performFetch()
}catch let error as NSError {
print(error)
}
return _fetchedResultsController!
}
<强>2。我有一个按钮可以添加操作:
@IBAction func didTouchAddChoiceButton(sender: UIButton) {
let choice = Choice.MR_createEntity() as! Choice
choice.id = GDUtils.CMUUID()
choice.decision = decision
NSManagedObjectContext.MR_defaultContext().MR_saveToPersistentStoreAndWait()
}
<强>3。添加该实体后。我有一个 Controller 来处理像这样更新的 tableView
func controller(controller: NSFetchedResultsController, didChangeObject anObject: AnyObject, atIndexPath indexPath: NSIndexPath?, forChangeType type: NSFetchedResultsChangeType, newIndexPath: NSIndexPath?) {
switch(type){
case .Insert:
tableView.insertRowsAtIndexPaths([newIndexPath!], withRowAnimation: .Top)
case .Delete:
tableView.deleteRowsAtIndexPaths([indexPath!], withRowAnimation: .Fade)
case .Update:
tableView.cellForRowAtIndexPath(indexPath!)
case .Move:
tableView.deleteRowsAtIndexPaths([indexPath!], withRowAnimation: .Left)
tableView.insertRowsAtIndexPaths([newIndexPath!], withRowAnimation: .Fade)
}
}
4.但是问题发生了:每次我尝试从 fetchedObjects 更改实体的属性时:
let chosenChoice = fetchedResultsController.objectAtIndexPath(currentIndextPath!) as! Choice
chosenChoice.name = tableCell.choiceName.text
我收到此消息:
CoreData: error: Serious application error. An exception was caught from the delegate of NSFetchedResultsController during a call to -controllerDidChangeContent:. Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (1) must be equal to the number of rows contained in that section before the update (1), plus or minus the number of rows inserted or deleted from that section (1 inserted, 0 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out). with userInfo (null)
谁能帮我弄清楚发生了什么?
最佳答案
iOS8 上的 NSFetchedResultsController 中存在一个错误,其中 FRC 调用 .Insert 而不是 .Update。我解决了这个问题,当在 iOS8 上调用 .Insert 时,我完全重新加载表格。
case .Insert:
guard let newIndexPath = newIndexPath else { return }
// iOS8 bug when FRC calls insert instead of Update
if #available(iOS 9, *) {
// insert item normally
} else {
// reload everything
}
关于ios - 核心数据与 NSFetchedResultsController :Invalid update with with a property in fetchObjects,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35009394/