Extending Your Core Data Classes

Like any framework, Core Data has a learning curve.  When you’re working your way up the curve it can feel like the curve never ends, but it does plateau.  There does come the time when you stop working so hard to understand the framework and you are actually being very productive.  And, if you’re not careful, you’ll come to appreciate and enjoy how much work Core Data does for you.

The primary tool you use when designing your data model is the Core Data Model Editor in Xcode.  The editor enables you to model objects, add attributes and relationships,  even preconfigure fetched results controllers.  All of this is in the visual designer.  That’s a lot of power all in one little corner of Xcode.

Core Data Model Editor

The editor does make it easy to create your model and it will automatically result in the necessary actual data store when your app is launched.  Core Data even verifies that your model matches the data store in the app.  There are lots of other features (such as versioning) the Core Data provides.

The Core Data Model Editor provides the means to generate your own Swift (or Objective-C) classes.  The editor creates an individual source file for each class (a header file too if you’re using Objective-C).  To create the class you simply select the Entities in the Core Data Model Editor and then use the “Editor” menu to choose “Create NSManagedObject Subclass…”.  The dialog boxes will guide you through the details of creating and saving the actual source code files.

Core Data Model Editor Generate

Your newly generated classes are very straightforward.  The class simply declares each of the properties in your class.  Xcode is kind enough to use the appropriate data types so that you can get back to the brain work of creating your app.

Given the class, you can then create instance through your managed object context and then work with class instances.  The framework creates NSManagedObject instances that you can easily cast into the type of your class.  Again, pretty straightforward.

In many cases, you’ll want to be extending your Core Data classes to add functionality. You can, of course, edit the actual class files that Xcode generated for you.  That’s an acceptable practice.  And it works well…if you know that you won’t be updating your model.  However, if you will be updating your model then it’s easiest to do in the Core Data Model Editor.  But if you have Xcode recreated the class files then you’ll lose your additions. 

I recommend creating a separate source file in which you can declare your extensions to your Core Data classes.  By using a separate source file you can customize to your heart’s content and still use the Core Data Model Editor to update your models. 

In this example, we are extending the Employee class in two different ways.  First, we are overriding awakeFromInsert() in order to set a default value for every instance of the class.  The awakeFromInsert method is called one time for every NSManagedObject instance so it is the appropriate place for you to implement any default values. In this example we are simply assigning the current date/time to the createdAt property.  The second way in which we are extending the Employee class is by adding a brand new method, fullName(), that provides some new functionality that is specific to the class.  You can refer to the instance’s properties by using the self qualifier.


Putting your NSManagedObject extensions into separate source files helps you maintain the best of both worlds:  use the Core Data Model Editor and extend your classes.  Core Data and Swift together make it straightforward to write some very effective code and still use the tools to do some of the grunt work for you.
This entry was posted in Core Data, Technical. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *


*