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.

Setting Up the Stack

This video was last updated on Jul 19 2022

Create a Core Data stack for use in the RocketLaunches app!

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.

Now that you have a better understanding of the moving parts let’s actually write some code.

import CoreData
struct PersistenceController {

}
struct PersistenceController {
  static let shared = PersistenceController()

  static var preview: PersistenceController = {
    let result = PersistenceController(inMemory: true)
    let viewContext = result.container.viewContext
    
    // Dummy data will go here later
    
    do {
      try viewContext.save()
    } catch {
      let nsError = error as NSError
      fatalError("Unresolved error \(nsError), \(nsError.userInfo)")
    }
    return result
  }()
}
  let container: NSPersistentContainer

  init(inMemory: Bool = false) {

	}  
let container = NSPersistentContainer
container = NSPersistentContainer(name: "RocketLaunches")
if inMemory {
  // swiftlint:disable:next force_unwrapping
  container.persistentStoreDescriptions.first!.url = URL(fileURLWithPath: "/dev/null")
}
container.loadPersistentStores { _, error in }
if let error = error as NSError? {
  fatalError("Unresolved error \(error), \(error.userInfo)")
}
  container.viewContext.automaticallyMergesChangesFromParent =
    true

  container.viewContext.name = "viewContext"
  /// - Tag: viewContextMergePolicy
  container.viewContext.mergePolicy = 
    NSMergeByPropertyObjectTrumpMergePolicy
  container.viewContext.undoManager = nil
  container.viewContext.shouldDeleteInaccessibleFaults = true

Reviews

Comments