Core Data: Beyond the Basics

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

Part 2: Advanced Core Data

16. Saving Launches Concurrently

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: 15. Saving Launches with Batch Operations Next episode: 17. Delete Rules

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.

Welcome back! In the last episode, you created BatchInsertRequests to get your SpaceXLaunch information into the database in one request. But how can you do this so it doesn’t interfere with the main queue where user interactions take place? Long running or CPU intensive operations can be done in the background. Core Data has had support for background operations for a while now, but with iOS 15 they adopted the asynchronous API that was introduced throughout other frameworks. Let’s see how you can combine past and present to get your data stored and be performant at the same time.

launch.addToList(list)
"ANY %K == %@"
@NSManaged public var list: Set<RocketLaunchList>
private func newTaskContext() -> NSManagedObjectContext {
	let taskContext = container.newBackgroundContext()
	taskContext.mergePolicy = NSMergeByPropertyObjectTrumpMergePolicy
	return taskContext
}
let taskContext = newTaskContext()
taskContext.name = "importContext"
taskContext.transactionAuthor = "importLaunches"
container.performBackgroundTask { context in 

}
try taskContext.performAndWait {
	// Use a batch insert request to add the launches
  let batchInsertRequest = createBatchInsertLaunchRequest(from: launchCollection)
  if let fetchResult = try?
    taskContext.execute(batchInsertRequest),
    let batchInsertResult = fetchResult as? NSBatchInsertResult,
    let success = batchInsertResult.result as? Bool, 
    success {
      return
    }
  throw LaunchError.batchInsertError
}
var list: SpaceXLaunchList!
try taskContext.performAndWait {
  let fetchRequest = SpaceXLaunchList.fetchRequest()
  fetchRequest.predicate = NSPredicate(format: "title == %@", listName)
  let results = try taskContext.fetch(fetchRequest)
  if let fetchedList = results.first {
		list = fetchedList
  }
}
try taskContext.perform {
	// Use a batch insert request to add the fairings
	let batchInsertRequest = createBatchInsertRelationshipRequest(from: fairings, for: SpaceXFairings.self)
	if let fetchResult = try? taskContext.execute(batchInsertRequest),
	let batchInsertResult = fetchResult as? NSBatchInsertResult,
	let success = batchInsertResult.result as? Bool, success {
	  return
	}
	throw LaunchError.batchInsertError
}
try taskContext.perform {
	//around links batch insert
}
try await taskContext.perform {
	//around links for loop
}
try await taskContext.perform
func importLaunches(from launchCollection: [SpaceXLaunchJSON], to listName: String) async throws
try await PersistenceController.shared.importLaunches(from: launches, to: "All")