Core Data: Beyond the Basics

Jul 26 2022 · Swift 5.5, iOS 15, Xcode 13.3.1

Part 1: Fetching & Displaying Launches

08. Compound Predicates

Episode complete

Play next episode

Next
About this episode

Leave a rating/review

See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 07. Adding Launches to Lists Next episode: 09. Challenge - Adding Tags

Get immediate access to this and 4,000+ other videos and books.

Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and 4,000+ professional videos in a single subscription, it's simply the best investment you can make in your development career.

Learn more Already a subscriber? Sign in.

Heads up... You've reached locked video content where the transcript will be shown as obfuscated text.

Instead of working with relationships another way you can fetch the launches for a particular list is by using something you’re already familiar with - predicates.

static func launches(in list: RocketLaunchList) -> FetchRequest<RocketLaunch> {}
let nameSortDescriptor = NSSortDescriptor(key: "name", ascending: true)
let launchDateSortDescriptor = NSSortDescriptor(key: "launchDate", ascending: false)
let listPredicate = NSPredicate(format: "%K == %@", "list.title", list.title!)
let isViewedPredicate = NSPredicate(format: "%K == %@", "isViewed", NSNumber(value: false))
let combinedPredicate = NSCompoundPredicate()
let combinedPredicate = NSCompoundPredicate(andPredicateWithSubpredicates: [listPredicate, isViewedPredicate])
return FetchRequest(entity: RocketLaunch.entity(),
  sortDescriptors: [nameSortDescriptor, launchDateSortDescriptor],
  predicate: combinedPredicate)
var launchesFetchRequest: FetchRequest<RocketLaunch>
init(launchList: RocketLaunchList) {
	self.launchList = launchList
	self.launchesFetchRequest = RocketLaunch.launches(in: launchList)
}
ForEach(launches, id: \.self) { launch in }