在视图控制器之间传递数据时如何过滤NSPredicate

时间:2019-03-25 12:40:13

标签: swift core-data nspredicate

在collectionView单元格上进行轻拍应该只传递NSFetchRequest中的过滤属性,而是将每个对象传递给下一个viewController(本身就是tableViewController)

按属性过滤

var album: Album?

    func fetchAlbumDetailResults() -> NSFetchedResultsController<Album> {
        guard let producer = album?.producer else { return fetchAlbumDetailResults() }

        let fetchRequest: NSFetchRequest<Album> = Album.fetchRequest()
        fetchRequest.sortDescriptors = [
            NSSortDescriptor(key: "producer", ascending: true, selector: #selector(NSString.caseInsensitiveCompare(_:)))
        ]
        fetchRequest.predicate = NSPredicate(format: "producer == %@", producer)

        let frc = NSFetchedResultsController(fetchRequest: fetchRequest,
                                             managedObjectContext: mainContext,
                                             sectionNameKeyPath: nil,
                                             cacheName: nil)
        return frc
    }

预期结果:仅获取由单个制作人制作的专辑

实际结果:获取每张专辑

0 个答案:

没有答案