swiftcore-datansorderedset

Can't perform collection evaluate with non-collection object


When I perform segue I pass all seasons from a selected show to the next ViewController.

class ShowsViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Request Items with type = Movie
        let request: NSFetchRequest<TelevisionShow> = TelevisionShow.fetchRequest()
        allShows = try! CoreDataStack.context.fetch(request)
        allShows = allShows.sorted(by: { $0.title! <  $1.title! })
    }

    @IBOutlet private weak var collectionView: UICollectionView!
}

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

        // Create a variable that you want to send based on the destination view controller

        selectedShow = allShows[indexPath.item]

        // This will perform the segue and pre-load the variable for you to use
        self.performSegue(withIdentifier: "toSeasonsViewController", sender: self)

    }

If I try a fetch request with NSPredicate, I get crash. (2019-02-04 11:10:48.766456+0100 TV[16295:659978] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Can't perform collection evaluate with non-collection object.')

class SeasonsViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        let request: NSFetchRequest<Season> = Season.fetchRequest()
        request.returnsObjectsAsFaults = false
        request.predicate = NSPredicate(format: "SUBQUERY(televisionShow, $m, ANY $m.seasons IN %@).@count > 0",(pTelevisionShow?.seasons)!)

        allSeasons = try! CoreDataStack.context.fetch(request)
        allSeasons = allSeasons.sorted(by: { $0.number! <  $1.number! })
    }

    @IBOutlet private weak var collectionView: UICollectionView!

    var pTelevisionShow: TelevisionShow?
}

xcdatamodeld The problem is that first time I'm selecting a tvshow and perform segue, it works well, if I go back and choose another tvshow, it throws that error.

+

class AppDelegate: UIResponder, UIApplicationDelegate { Thread 1: signal SIGABRT

libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

pTelevisionShow?.seasons is NSOrderedSet

xcdatamodeld The solution:

allSeasons = pTelevisionShow?.seasons!.sorted(by: { ($0 as! Season).number! < ($1 as! Season).number! }) as! [Season]

Solution

  • *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Can't perform collection evaluate with non-collection object.')

    Looks like your predicate is returning a single object rather than a collection of objects.

    So your predicate logic NSPredicate(format: "SUBQUERY(televisionShow, $m, ANY $m.seasons IN %@).@count > 0",(pTelevisionShow?.seasons)!) is returning a single element for some reason.

    However, in your case, since you're passing pTelevisionShow forward, your structure already has reference to the required seasons dataset and can be directly accessed, so you don't need a fetchRequest on Season again.


    Remove the following code:

    let request: NSFetchRequest<Season> = Season.fetchRequest()
    request.returnsObjectsAsFaults = false
    request.predicate = NSPredicate(format: "SUBQUERY(televisionShow, $m, ANY $m.seasons IN %@).@count > 0",(pTelevisionShow?.seasons)!)
    

    Change and keep:

    allSeasons = pTelevisionShow?.seasons.sorted(by: { $0.number! < $1.number! })