NSFetchedResultsController看不到插入(或更新)

时间:2016-04-09 07:59:40

标签: ios swift uitableview core-data nsfetchedresultscontroller

我有一个非常基本的UITableView,用CoreData: error: Serious application error. An exception was caught from the delegate of NSFetchedResultsController during a call to -controllerDidChangeContent:. Invalid update: invalid number of sections. The number of sections contained in the table view after the update (3) must be equal to the number of sections contained in the table view before the update (2), plus or minus the number of sections inserted or deleted (0 inserted, 0 deleted). with userInfo (null) 向用户显示数据,允许用户添加新实体等等。

然而,每当我添加一个新实体时,我的应用程序崩溃(或有时只是警告),并显示以下消息:

numberOfRows

请注意,即使(0 inserted, 0 deleted)已经从2更新为3,插入/删除操作仍然会显示NSFetchedResultsController。所以我最好的理解是func fetch(frcToFetch: NSFetchedResultsController) { do { try frcToFetch.performFetch() } catch { return } } func fetchRequest() -> NSFetchRequest { // Initialize Fetch Request let fetchRequest = NSFetchRequest(entityName: "ItemInfo") // Add Sort Descriptors let nameSortDescriptor = NSSortDescriptor(key: "iName", ascending: true) fetchRequest.sortDescriptors = [nameSortDescriptor] return fetchRequest } func getFRC() -> NSFetchedResultsController { if let context = self.context{ fetchedResultsController = NSFetchedResultsController(fetchRequest: fetchRequest(), managedObjectContext: context, sectionNameKeyPath: "iName.stringGroupByFirstInitial", cacheName: nil) fetchedResultsController.delegate = self } return fetchedResultsController } func controllerWillChangeContent(controller: NSFetchedResultsController) { tableView.beginUpdates() } func controllerDidChangeContent(controller: NSFetchedResultsController) { tableView.endUpdates() } func controller(controller: NSFetchedResultsController, didChangeObject anObject: AnyObject, atIndexPath indexPath: NSIndexPath?, forChangeType type: NSFetchedResultsChangeType, newIndexPath: NSIndexPath?) { switch (type) { case .Insert: if let indexPath = newIndexPath { tableView.insertRowsAtIndexPaths([indexPath], withRowAnimation: .Fade) } break; case .Delete: if let indexPath = indexPath { tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade) } break; case .Update: if let indexPath = indexPath { let cell = tableView.cellForRowAtIndexPath(indexPath)! as UITableViewCell configureCell(cell, atIndexPath: indexPath) } break; case .Move: if let indexPath = indexPath { tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade) } if let newIndexPath = newIndexPath { tableView.insertRowsAtIndexPaths([newIndexPath], withRowAnimation: .Fade) } break; } } override func numberOfSectionsInTableView(tableView: UITableView) -> Int { if let sections = fetchedResultsController.sections { return sections.count } return 0 } override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { if let sections = fetchedResultsController.sections { let sectionInfo = sections[section] return sectionInfo.numberOfObjects } return 0 } 没有注意到这些变化。

我的NSFetchedResultsController代码是:

let entity: NSEntityDescription.entityForName("ItemInfo", inManagedObjectContext: self.context!)!

let record = NSManagedObject(entity: entity, insertIntoManagedObjectContext: self.context!)

record.setValue(name, forKey: "iName")
record.setValue(self.billingMode.text, forKey: "iBillingMode")

do {
            // Save Record
            try record.managedObjectContext?.save()
            try self.context!.save()
            // Dismiss View Controller
            dismissViewControllerAnimated(true, completion: nil)

        } catch {
            let saveError = error as NSError
            print("\(saveError), \(saveError.userInfo)")

            // Show Alert View
            showAlertWithTitle("Unexpected Error", message: "Your data could not be saved. Please try again later.", cancelButtonTitle: "Done")
        }

插入新记录的代码是:

self.context

请注意,NSFetchedResultsController变量是从具有from yaml.resolver import Resolver Resolver.add_implicit_resolver( 'tag:yaml.org,2002:int', re.compile(r'''^(?:[-+]?0b[0-1_]+ |[-+]?0o?[0-7_]+ |[-+]?0[0-7_]+ |[-+]?(?:0|[1-9][0-9_]*) |[-+]?0x[0-9a-fA-F_]+ |[-+]?[1-9][0-9_]*(?::[0-5]?[0-9])+)$''', re.X), list('-+0123456789')) 的实际或主视图控制器传递的。

1 个答案:

答案 0 :(得分:0)

请注意,问题在于部分的数量而不是行的数量。你需要实现:

(void)controller:(NSFetchedResultsController *)controller didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo
相关问题