2016-01-26 17 views
2

我有Xcode中報告了嚴重的問題,一個屬性無效更新:CoreData與NSFetchResultController:與在fetchObjects

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後添加這個實體。我有一個控制器來處理更新的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.But發生問題:每次我試圖改變從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)

任何人都可以幫助我弄清楚發生了什麼?

+0

你得到這個錯誤是因爲在fetchResultsController的託管objectContext中沒有添加或刪除行。 – Dari

回答

2

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 
    } 
1

確保您分配給NSFetchResultsController的managedOjectContext與您創建新的Choice NSManagedObject的位置相同。

在 「情況下.Update」 代碼給你的tableview細胞ADDED

。並且您需要更新tableView單元格數據以避免發生該錯誤。至少改變/(假裝改變)在細胞中。

cell = tableView.cellForRowAtIndexPath(indexPath!) 
cell.choiceName.text = "empty" 
+0

我使用MagicalRecord,並且總是分配了MR_DefaultContext – nguyendang

+0

請參閱上面回覆的添加部分:我認爲這個錯誤是因爲在更新選項managedObject時,表視圖中沒有任何變化。它會向nsfetchresultcontroller拋出更新通知,但如果不更改單元格,則可能會出現此錯誤。 – Dari