NSFetchedResultsControllerDelegate:controllerDidChangeContent在iOS 8中不起作用

时间:2015-02-01 06:35:22

标签: ios uitableview swift core-data ios8

我正在尝试在UITableView中使用NSFetchedResultsController ..但由于某种原因,当我添加新条目时,NSFetchedResultsControllerDelegate不会被触发。这是完整的代码

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

}

当我向上下文添加条目并保存内容时,不会调用controllerWillChangeContentcontrollerDidChangeContent。我确保上下文已保存,因为如果我重新启动应用程序,我可以看到最近添加的条目 任何人都可以找到上面编写的代码的任何问题吗?

1 个答案:

答案 0 :(得分:1)

我注意到你设置了两次委托。一旦进入计算属性,一次进入viewDidLoad。我认为您的问题可能源于您首先在计算属性中为aFetchedResultsController设置它,因此,在调用aFetchedResultsController委托方法时调用委托方法(他们不是)。删除计算属性中的委派任务(aFetchedResultsController.delegate = self)应解决此问题。