Swift:NSArrayM controllerWillChangeContent:]:无法识别的选择器发送到实例

时间:2016-05-18 12:26:05

标签: ios swift nsfetchedresultscontroller

在我的控制器中,我有两个 UICollectionView有两个不同的NSFetchedResultsControllers

这是我在viewDidLoad:中创建它的方式:

private func setupFetchedResultsControllers() {

    let currentStudent = BWSettings.sharedSettings.currentUser as! BWCoreDataStudent
    let context = NSManagedObjectContext.MR_defaultContext()

    let topFetchReguest = NSFetchRequest(entityName: "BWCoreDataWishlistBook")
    let positionDescriptor = NSSortDescriptor(key: "position", ascending: true)

    topFetchReguest.sortDescriptors = [positionDescriptor]

    topFetchedResultsController = NSFetchedResultsController(fetchRequest: topFetchReguest, managedObjectContext: context, sectionNameKeyPath: nil, cacheName: nil)
    topFetchedResultsController.fetchRequest.predicate = NSPredicate(format: "student.id = %lld AND position != nil", currentStudent.id)
    topFetchedResultsController.delegate = BWStudentWishlistFetchedResultsControllerDelegate(collectionView: topCollectionView, studentWishlistContainerViewController: self)

    try! topFetchedResultsController.performFetch()

    let bottomFetchReguest = NSFetchRequest(entityName: "BWCoreDataWishlistBook")
    let addedAtDescriptor = NSSortDescriptor(key: "createdAt", ascending: true)

    bottomFetchReguest.sortDescriptors = [addedAtDescriptor]

    bottomFetchedResultsController = NSFetchedResultsController(fetchRequest: bottomFetchReguest, managedObjectContext: context, sectionNameKeyPath: nil, cacheName: nil)
    bottomFetchedResultsController.fetchRequest.predicate = NSPredicate(format: "student.id = %lld AND position = nil", currentStudent.id)
    bottomFetchedResultsController.delegate = BWStudentWishlistFetchedResultsControllerDelegate(collectionView: bottomCollectionView, studentWishlistContainerViewController: self)

    try! bottomFetchedResultsController.performFetch()
}

这是我的自定义代表:

class BWStudentWishlistFetchedResultsControllerDelegate: NSObject, NSFetchedResultsControllerDelegate {

    private var collectionView: UICollectionView!
    private var collectionViewChanges = [[NSFetchedResultsChangeType: [NSIndexPath]]]()

    private weak var studentWishlistContainerViewController: BWStudentWishlistContainerViewController!

    init(collectionView: UICollectionView, studentWishlistContainerViewController: BWStudentWishlistContainerViewController) {

        self.collectionView = collectionView
        self.studentWishlistContainerViewController = studentWishlistContainerViewController
    }

    //MARK: - NSFetchedResultsControllerDelegate
    //here are my custom default methods
}

这是一条完整的错误消息:

  

***由于未捕获的异常终止应用程序' NSInvalidArgumentException',原因:' - [__ NSArrayM controllerWillChangeContent:]:无法识别的选择器发送到实例0x14ec0d930'

为什么会这样?

1 个答案:

答案 0 :(得分:1)

您的错误是您的NSFetchedResultsController委托正在取消分配。 而不是:

 topFetchedResultsController.delegate = BWStudentWishlistFetchedResultsControllerDelegate(collectionView: topCollectionView, studentWishlistContainerViewController: self)

您需要使用属性保留对委托的引用:

var fetchDelegate = BWStudentWishlistFetchedResultsControllerDelegate(..)
...
topFetchedResultsController.delegate = self.fetchDelegate

通过这种方式,委托在 setupFetchedResultsControllers()范围

结束时发布