Core Data: Beyond the Basics

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

Part 1: Fetching & Displaying Launches

07. Adding Launches to Lists

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: 06. Modeling Relationships Next episode: 08. Compound Predicates

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’re accessing parts of this content for free, with some sections shown as obfuscated text.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

Welcome back! Now that you have a more complex object graph, you have to rework the app a bit to handle it. First, you’ll need to go to your simulator and get rid of the app on the launch screen. Any RocketLaunches you created prior to the last video are incompatible with the new model - remember that instances of RocketLaunch are now required to have an associated list. If you build and run, the app will try to look for that list and crash instead.

RocketLaunchList.create(withTitle: self.text, in: self.viewContext)
@FetchRequest(sortDescriptors: []) 
var launchLists: FetchedResults<RocketLaunchList>
ForEach(launchLists, id: \.self) { launchList in }
Text(launchList.title ?? "")
NavigationLink(destination: LaunchesView()) {
  CircularImageView(color: .red)
  Text(launchList.title)
}
let launchList: RocketLaunchList
let context = PersistenceController.preview.container.viewContext
let newLaunchList = RocketLaunchList(context: context)
newLaunchList.title = "Preview List"
return LaunchesView(launchList: newLaunchList).environment(\.managedObjectContext, context)
NavigationLink(destination: LaunchesView(launchList: launchList)) {}
static func createWith(
	name: String,
	notes: String,
	launchDate: Date,
	isViewed: Bool,
	launchpad: String,
**	in list: RocketLaunchList,
	using managedObjectContext: NSManagedObjectContext
	) {
	let launch = RocketLaunch(context: managedObjectContext)
	launch.name = name
	launch.notes = notes
	launch.launchDate = launchDate
	launch.isViewed = isViewed
	launch.launchpad = launchpad
**	launch.list = list
	
	do {
	  try managedObjectContext.save()
	} catch {
	  let nserror = error as NSError
	  fatalError("Unresolved error \(nserror), \(nserror.userInfo)")
	}
}
@NSManaged var list: RocketLaunchList
let launchList: RocketLaunchList

RocketLaunch.createWith(
	name: self.text,
	notes: self.notes,
	launchDate: self.launchDate,
	isViewed: false,
	launchpad: self.launchPad,
	in: self.launchList,
	using: self.viewContext)
let context = PersistenceController.preview.container.viewContext
let newLaunchList = RocketLaunchList(context: context)
newLaunchList.title = "Preview List"
return LaunchCreateView(launchList: newLaunchList)
  .environment(\.managedObjectContext, context)
let launchList: RocketLaunchList

LaunchCreateView(launchList: self.launchList)
NewLaunchButton(isShowingCreateModal: $isShowingCreateModal, launchList: self.launchList)
Triple Launch
Launch Date: 6/1/22
CA Launches