Core Data Codegen Explained!
Starting from Xcode8, Apple has added a new
Codegen setting in the Xcode data model editor to help developer manage and maintain their
NSManagedObject subclasses. The
Codegen setting consist of 3 possible configurations:
- Class Definition
These 3 configurations might look a bit confusing at first, and I found out that it is actually quite difficult to find a good article that explain the differences between each configuration and which one to use.
In this article I will go through each configuration and analyse the differences between each of them and I will also give some suggestion on which one you should use and how you should use it for your Core Data project. Without further ado let’s get started!
This is the default behaviour prior to Xcode8 where developer have to manually create and maintain the changes of the
You can try to create a sample core data project, add a new entity call
TestEntity and generate
NSManagedObject subclass following the step in this link.
If you follow the step correctly, Xcode will generate 2 files:
After these 2 files been generated, you should be able to use
TestEntity in your project. Try copy the following line of code into your app delegate, your project should compile without any error.
let _ = TestEntity(context: persistentContainer.viewContext)
This configuration is the default
Codegen configuration when you create an entity in the data model editor. With this configuration, Xcode will automatically generate the required
NSManagedObject subclass as part of the project’s derived data.
To see this in action, open the sample core data project that you created previously and delete both…