Get immediate access to this and over 1,500+ other videos and books.

Boost your skills with a raywenderlich.com Beginner subscription. With over 60+ video courses and our core foundational programming books bundled in one subscription, it’s simply the best investment you can make in your development career.

Inserting Data Into the Context

This video was last updated on Jul 19 2022

Now that you have a RocketLaunch class you can use learn how to populate it and save data to the persistent store.

Contributors

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

You can unlock the rest of this video course, and our entire catalogue of books and videos, with a raywenderlich.com Professional subscription.

In the last video you created a managed object subclass for the RocketLaunch entity. Now you can use this class to create instances of a RocketLaunch for users to save. If you build and run the app, you’ll see a new rocket launch button at the bottom of the screen. When you tap on that you’re presented with a modal view to enter details about the rocket launch.

RocketLaunch(
let persistenceController = PersistenceController.shared
ContentView()
	.environment(\.managedObjectContext, persistenceController.container.viewContext)
import CoreData
@Environment(\.managedObjectContext) var viewContext
let launch = RocketLaunch(context: self.viewContext)
launch.name = self.name
launch.notes = self.notes
launch.launchDate = self.launchDate
launch.isViewed = self.isViewed
launch.launchpad = self.launchpad
do {
  try self.viewContext.save()
}
catch {
  let nserror = error as NSError
  fatalError("Unresolved error \(nserror), \(nserror.userInfo)")
}
  static func createWith(
    name: String,
    notes: String,
    launchDate: Date,
    isViewed: Bool,
    launchpad: String,
    using managedObjectContext: NSManagedObjectContext
  ) {}
RocketLaunch.createWith(
	name: self.name,
	notes: self.notes,
	launchDate: self.launchDate,
	isViewed: false,
	launchpad: self.launchpad,
  using: self.viewContext)
A really cool launch
My back yard
I really hope this takes off!

Reviews

Comments