2016-04-09 24 views
1

私はUITableViewのユーザーにデータを表示する非常に基本的なNSFetchedResultsControllerを持っており、ユーザーは新しいエンティティなどを追加できます。でもnumberOfRowsは、2から3に挿入を更新してきたと思っNSFetchedResultsControllerに挿入(または更新)が表示されません

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) 

注意:次のメッセージと

しかし、私は新しいエンティティを追加するたびに、私のアプリがクラッシュ(または時々ちょうど警告しています) /削除事項はまだ(0 inserted, 0 deleted)と言います。だから私の最善の理解は、NSFetchedResultsControllerが変更や何かに気づいていないということです。

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 
} 

、新しいレコードを挿入するコードは次のとおりです。self.context変数があり、実際のまたはマスタービューコントローラから渡されていること

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") 
     } 

注意NSFetchedResultsController。問題は、セクションの数である

+0

あなたのアプリを削除してもう一度実行してみてください。 – Khuong

+0

まあ、多くのデータが入っているため、アプリを削除できません。私は更新の途中で、バックアップ機能は現在壊れています。したがって、削除はオプションではありません。私は 'Clean Build Folder'を実行して実行しようとしましたが、問題は依然として続きます。 –

+2

問題は*行*の数ではなく、*セクションの数であることに注意してください。あなたは ' - (void)controller:(NSFetchedResultsController *)コントローラdidChangeSection:(id )sectionInfo'を実装しましたか? – pbasdf

答えて

0

注ないの数。

(void)controller:(NSFetchedResultsController *)controller didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo 
関連する問題