2015-02-01 3 views
1

Я пытаюсь использовать NSFetchedResultsController в UITableView .. Но по какой-то причине NSFetchedResultsControllerDelegate не запускается, когда я добавляю новую запись. Вот полный кодNSFetchedResultsControllerDelegate: controllerDidChangeContent не работает в iOS 8

import UIKit 
import CoreData 

let coreDataStack = CoreDataStack() 
class CDListViewController: UITableViewController, NSFetchedResultsControllerDelegate { 


    var fetchedResultsController: NSFetchedResultsController { 
     if _fetchedResultsController != nil { 
      return _fetchedResultsController! 
     } 
     let fRequest = fetchRequest() 

     let aFetchedResultsController = NSFetchedResultsController(fetchRequest: fRequest, managedObjectContext: coreDataStack.managedObjectContext!, sectionNameKeyPath: nil, cacheName: nil) 

     aFetchedResultsController.delegate = self 
     _fetchedResultsController = aFetchedResultsController 

     var error: NSError? = nil 
     if !_fetchedResultsController!.performFetch(&error) { 
     } 
     println(error) 

     return _fetchedResultsController! 
    } 

    var _fetchedResultsController: NSFetchedResultsController? = nil 

    func fetchRequest() -> NSFetchRequest { 
     let fetchRequest = NSFetchRequest(entityName: "Menu") 

     // Set the batch size to a suitable number. 
     fetchRequest.fetchBatchSize = 2 

     // Edit the sort key as appropriate. 
     let sortDescriptor = NSSortDescriptor(key: "createdAt", ascending: false) 

     fetchRequest.sortDescriptors = [sortDescriptor] 
     return fetchRequest 
    } 


    override func viewDidLoad() { 
     super.viewDidLoad() 
     fetchedResultsController.delegate = self 
     print(fetchedResultsController) 
//  fetchedResultsController?.performFetch(nil) 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 

    } 

    override func tableView(tableView: UITableView, editingStyleForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCellEditingStyle { 
     return UITableViewCellEditingStyle.Delete 
    } 

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
     return 1 
    } 

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     let menus = fetchedResultsController.fetchedObjects as [Menu] 
     return menus.count 
    } 


    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell 
     let curr = fetchedResultsController.objectAtIndexPath(indexPath) as Menu 
     // Configure the cell... 
     cell.textLabel?.text = curr.text 
     return cell 
    } 



    // Override to support conditional editing of the table view. 
    override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool { 
     // Return NO if you do not want the specified item to be editable. 
     return true 
    } 



    // Override to support editing the table view. 

    func controllerWillChangeContent(controller: NSFetchedResultsController) { 
     println("Coming in here") 
     self.tableView.beginUpdates() 
    } 

    func controllerDidChangeContent(controller: NSFetchedResultsController) { 
     self.tableView.endUpdates() 
    } 




    // Override to support editing the table view. 
    override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) { 
     let entry = self.fetchedResultsController.objectAtIndexPath(indexPath) as Menu 
     coreDataStack.managedObjectContext?.deleteObject(entry) 
     coreDataStack.saveContext() 
    } 

    func controller(controller: NSFetchedResultsController, didChangeObject anObject: AnyObject, atIndexPath indexPath: NSIndexPath?, forChangeType type: NSFetchedResultsChangeType, newIndexPath: NSIndexPath?) { 
     switch(type) { 
     case NSFetchedResultsChangeType.Insert : self.tableView.insertRowsAtIndexPaths([newIndexPath!], withRowAnimation: UITableViewRowAnimation.Automatic) 
      break 
     case NSFetchedResultsChangeType.Delete : self.tableView.deleteRowsAtIndexPaths([indexPath!], withRowAnimation: UITableViewRowAnimation.Automatic) 
      break 
     case NSFetchedResultsChangeType.Update : self.tableView.reloadRowsAtIndexPaths([indexPath!], withRowAnimation: UITableViewRowAnimation.Automatic) 
     default: 
      println("Nothing") 
     } 
    } 

    func controller(controller: NSFetchedResultsController, didChangeSection sectionInfo: NSFetchedResultsSectionInfo, atIndex sectionIndex: Int, forChangeType type: NSFetchedResultsChangeType) { 
     switch(type) { 
     case NSFetchedResultsChangeType.Insert : self.tableView.insertSections(NSIndexSet(index: sectionIndex), withRowAnimation: UITableViewRowAnimation.Automatic) 
      break 
     case NSFetchedResultsChangeType.Delete : self.tableView.deleteSections(NSIndexSet(index: sectionIndex), withRowAnimation: UITableViewRowAnimation.Automatic) 
      break 
     default: 
      println("Nothing") 
     } 
    } 

} 

При добавлении записи в контекст и сохранить содержание ни controllerWillChangeContent, ни controllerDidChangeContent который вызывается. Я убедился, что контекст сохранен, потому что, если я перезапущу приложение, я вижу недавно добавленную запись.
Может ли кто-нибудь найти какие-либо проблемы с кодом, написанным выше?

ответ

0

Я заметил, что вы установили делегат дважды. Один раз внутри вычисленного свойства и один раз в viewDidLoad. Я думаю, что ваша проблема может возникнуть из-за того, что вы сначала задали ее для aFetchedResultsController в вычисляемом свойстве, и, таким образом, методы делегата вызывается при вызове методов делегатов aFetchedResultsController (чего они не являются). При удалении задания делегирования (aFetchedResultsController.delegate = self) в вычисленном свойстве необходимо устранить эту проблему.

+0

Выяснил это. У меня было несколько контекстов managedObject. Создание единого контекста управляемого объекта позволило решить проблему – Shrikar

Смежные вопросы