2015-02-01 3 views
1

NSFetchedResultsController를 UITableView에서 사용하려고합니다.하지만 새 항목을 추가 할 때 NSFetchedResultsControllerDelegate가 해고되지 않습니다. 나는 문맥에 항목을 추가하고 controllerWillChangeContentcontrollerDidChangeContent가 모두 호출되는 내용을 저장하면 여기에 전체 코드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") 
     } 
    } 

} 

입니다. 애플 리케이션을 다시 시작하면 최근에 추가 된 항목을 볼 수 있기 때문에 컨텍스트가 저장되었는지 확인했습니다.
누구든지 위의 코드로 문제를 찾을 수 있습니까?

답변

0

위임을 두 번 설정했습니다. 계산 된 속성 내부 및 한 번 viewDidLoad에 있습니다. 귀하의 문제는 aFetchedResultsController에 대해 계산 된 속성에서 먼저 설정한다는 사실로부터 발생할 수 있으므로 aFetchedResultsController의 대리자 메서드가 호출 될 때 (즉, 그렇지 않은 경우) 대리자 메서드가 호출됩니다. 계산 된 속성에서 위임 할당 (aFetchedResultsController.delegate = self)을 삭제하면이 문제가 해결됩니다.

+0

알아 냈어. 나는 여러 managedObject 컨텍스트를 가졌다. 단일 관리 객체 컨텍스트를 생성하면 문제가 해결됨 – Shrikar