在我的控制器中,我有两个 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'
为什么会发生?
最佳答案
您的错误是您的NSFetchedResultsController委托正在取消分配。
代替 :
topFetchedResultsController.delegate = BWStudentWishlistFetchedResultsControllerDelegate(collectionView: topCollectionView, studentWishlistContainerViewController: self)
您需要使用属性来保留对委托的引用:
var fetchDelegate = BWStudentWishlistFetchedResultsControllerDelegate(..)
...
topFetchedResultsController.delegate = self.fetchDelegate
使用此方法,委托将在 setupFetchedResultsControllers()作用域的末尾释放