Composite attributes
- New type of attribute that encapsulates complex and custom data types
- You can nest them and now create them in the Core Data model editor
- There is a demo of how to adopt these new Composite Attributes
- Watching the demo I can only think, how will SwiftData handle this, and if it does already, how much easier will it be
Stage your migrations
- It is always preferable to do Lightweight migrations – it is built in and will happen automatically. Check out last year’s Evolve your app’s Schema – I captured my thoughts on that session here in the section on Core Data.
- When it is to complex, you can do a staged migration (this is new),
- For migration of non-conforming lightweight changes
- Simplify your app
- Provides some opportunities for your app to gain execution control during the migration to perform specific tasks
- You need to identify when Changes don’t conform with light weight migration
- You can manually review the chains
- You can try to open the store with the lightweight option using NSMigratePersistentStoresAutomaticallyOption and NSInferMappingModelAutomaticallyOption set to true.
- You’ll receive an hash error if they are not compatible
- Or you can use NSMappingModel.inferredMappingModel(forSourceModel:destinationModel:) which will either return the inferred model or nil if it cannot
- Decompose into a series of comforting migrations
- Describe the sequence of ordering using the new APIs
- Have Core Data execute an event loop to do the migration
- You need to identify when Changes don’t conform with light weight migration
- The complex model change that is described may work for my migration, I will have to see if I can recreate this with multiple models
- Adding Staged migration allows you to mix and match (in sequence) your model migration from both lightweight and custom migration stages.
- You create a .willMigrateHandler to do custom work during migration, for each fetch entity you will copy in the data and relate them.
Defer your migrations
- If your lightweight migrations take too much time, which can frustrate your users.
- You can now do deferred migrations at a later date, for example dropping indices or deleting a column – you can defer this clean up by setting
- NSPersistentStoreDeferredLightweightMigrationOptionKey to True – whit is only available for Sqlight, but is available back to iOS 14
- You can check the store’s metadata if that value is set and you can then process them by running NSPersistentStoreCoordinator.finishDeferredLightweightMigration()
- Consider using Background Task API to schedule this work
- You can combine deferred and staged migrations