无法执行收集与非集合对象评估

问题描述 投票:1回答:1

当我执行我赛格瑞通过所有季节从选定放映到下一个视图控制器。

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)

    }

如果我尝试用NSPredicate一个读取请求时,我得到崩溃。 (2019年2月4日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?
}

qazxsw POI的问题是,我第一次选择的电视节目,并执行SEGUE,它工作得很好,如果我回去,并选择另一tvshow,它抛出这个错误。

+

xcdatamodeld

pTelevisionShow?.seasons是NSOrderedSet class AppDelegate: UIResponder, UIApplicationDelegate { Thread 1: signal SIGABRT libc++abi.dylib: terminating with uncaught exception of type NSException (lldb)

解决方案:

xcdatamodeld
swift core-data nsorderedset
1个回答
0
投票

***终止应用程序由于未捕获的异常“NSInternalInconsistencyException”,理由是:“不能与非集合对象进行收集评估。”)

看起来像你的谓词返回一个对象,而不是对象的集合。

所以,你的谓词逻辑allSeasons = pTelevisionShow?.seasons!.sorted(by: { ($0 as! Season).number! < ($1 as! Season).number! }) as! [Season] 正在恢复由于某种原因,一个单一的元素。

然而,在你的情况,因为你传递NSPredicate(format: "SUBQUERY(televisionShow, $m, ANY $m.seasons IN %@).@count > 0",(pTelevisionShow?.seasons)!)前进,你的结构已经参照所需pTelevisionShow数据集,并可以直接访问,这样你就不会再需要在seasons一个fetchRequest


删除以下代码:

Season

改变和保持:

let request: NSFetchRequest<Season> = Season.fetchRequest()
request.returnsObjectsAsFaults = false
request.predicate = NSPredicate(format: "SUBQUERY(televisionShow, $m, ANY $m.seasons IN %@).@count > 0",(pTelevisionShow?.seasons)!)
© www.soinside.com 2019 - 2024. All rights reserved.