Migrate to Swiftdata


Generate model classes 

  • Use your managed Object Model to generate your SwiftData model, Open your model file in Xcode and select Editor -> Create SwiftData Code
    • You can also create the model from class.

Complete adoption

  • When fully monitoring you are replacing the core data stack with swiftData stack
  • Focus on understanding  your core data model designs, and check if they are supported in SwiftData
  • Highlights
    • Generate model classes, and delete the CoreData Managed Object model
    • Delete the persistent file that setup the stack
    • Setup ModelContainer – via modifier in a WindowGroup. This also setups the context for the environment
    • You insert new objects into the modelContext – it has an implicit save to persist data, you can remove implicit save
    • You can use Queries instead of FetchRequest

Coexists with Core Data

  • If you want coexistence you will end up with two different persistent stacks talking to the exiting Store
  • Before loading the persistent store you will need to make sure you set both stores with the same url and turn on .setOption(true as NSNumber, forKey: NSPersistentHistoryTrackingKey) – if you don’t do this, you will be in read only mode
  • SwiftData is only available in IOS17 and macOS Sonoma
  • Requirements
    • Use Namespace to ensure there is no collision – but Entities will stay the same
    • Keep Schema’s in sync – adding properties in the same way
    • You must keep track of Schema Versioning – Check out Model your Schema with SwiftData from WWDC23 
    • If you are using UIKit or AppKit you need to Core Data