Core Data: Beyond the Basics

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

Part 2: Advanced Core Data

18. Deleting Launch 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: 17. Delete Rules Next episode: 19. Storing Large Files

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

Navigate to your data model, the RocketLaunches.xcdatamodeld file. Earlier when you set up your entities you skipped a section or two. Let’s go back to those. Select the launches relationship in the RocketLaunchList entity and switch over to the Data Model Inspector. In the second section you should see a Delete Rule option and it should be set to Nullify by default. You’re going to change this to Cascade. Now when a list is deleted all associated launches will be deleted as well.

func delete(at offsets: IndexSet) {
	let lists = offsets.map { self.launchLists[$0] }
	do {
	  try PersistenceController.deleteList(list: lists[0])
	} catch {
	  print("Error deleting list")
	}
	}
.onDelete(perform: delete)
static func deleteList(list: RocketLaunchList) throws {
	let taskContext = shared.container.viewContext
	taskContext.delete(list)
	try taskContext.save()
}