【问题标题】:Can't perform collection evaluate with non-collection object无法使用非集合对象执行集合评估
【发布时间】:2019-02-03 22:12:19
【问题描述】:

当我执行 segue 时,我会将所有季节从选定的节目传递到下一个 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)

    }

如果我尝试使用 NSPredicate 获取请求,我会崩溃。 (2019-02-04 11:10:48.766456+0100 电视[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?
}

问题是我第一次选择一个电视节目并执行 segue 时,它​​运行良好,如果我返回并选择另一个电视节目,它会抛出该错误。

+

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

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

pTelevisionShow?.seasons 是 NSOrderedSet

解决办法:

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

【问题讨论】:

  • 看起来您的谓词返回的是单个对象而不是对象集合
  • 真的找不到问题所在,它第一次返回正确的季节数,然后如果我回去再次执行 segue:")}).@count > 0); sortDescriptors: ((null)); type: NSManagedObjectResultType; shouldRefreshRefetchedObjects: YES; ) 2019-02-04 10:09:14.910457+0100 TV[12970:579035] *** 由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“可以'不使用非集合对象执行集合评估。'"
  • 使用相关代码更新您的问题(删除与您当前问题无关的任何内容)。另外,放置一个异常断点并与我们分享它给出该错误的代码行。
  • @twixQ 其他电视节目有可用的季节信息吗?

标签: swift core-data nsorderedset


【解决方案1】:

*** 由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“无法使用非集合对象执行集合评估。”)

看起来您的谓词返回的是单个对象而不是对象集合。

所以您的谓词逻辑 NSPredicate(format: "SUBQUERY(televisionShow, $m, ANY $m.seasons IN %@).@count &gt; 0",(pTelevisionShow?.seasons)!) 出于某种原因返回单个元素。

但是,在您的情况下,由于您正在向前传递 pTelevisionShow,因此您的结构已经引用了所需的 seasons 数据集并且可以直接访问,因此您不需要 @987654325 上的 fetchRequest再次@。


删除以下代码:

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 = pTelevisionShow?.seasons.sorted(by: { $0.number! < $1.number! })

【讨论】:

    猜你喜欢
    • 2016-06-01
    • 2012-05-28
    • 2022-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多