2016-05-18 2 views
1

に送ら:スウィフト:NSArrayM controllerWillChangeContentは:]:認識されていないセレクタは、私は2つの異なる<code>NSFetchedResultsControllers</code>と <code>UICollectionView</code>年代を持って私のコントローラでは、インスタンス

これは私が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

http://stackoverflow.com/questions/3532861/core-data-app-crashing-with-controllerwillchangecontent-unrecognized-selector?rq=1 – Rajesh

+0

私はこの質問を読んだが、解決策は何か実際には?さらに、** Objective-C ** lanagugeがあり、ここに** Swift **があります。 –

+0

あなたは 'try! 'を使っています。それはうまくいかなかったので、* try catch blocks *を使ってみてください。そのため、アプリケーションはクラッシュすることはありませんが、より詳細な例外がスローされます。 – Binarian

答えて

1

あなたのエラーは、NSFetchedResultsControllerデリゲートの割り当てが解除されていることです。 の代わりに:)setupFetchedResultsControllersの最後(にリリースされたこのデリゲートを行うためのあなたの方法で

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

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

あなたは、プロパティを使用して、デリゲートへの参照を保持する必要があります範囲

+0

あなたは完全に正しいです:) –

関連する問題