Thursday, November 29, 2018 [Tweets] [Favorites]

Effective Core Data With Swift

Tom Harrington (PDF):

Core Data brings a lot of power to an app and continues to evolve, but it can have rough spots when you’re working in Swift. What if you want to save an enum pr a struct? Does it help if your data is Codable? What’s the best way to create Swift-friendly model classes? This session will cover techniques and gotchas for integrating Core Data with your Swift code

It’s a bit of a mystery to me why Xcode’s generated code for Core Data continues to be so unhelpful compared with mogenerator.

Another option for his enum example is that, if you have the ability to change the enum’s definition, you can make it @objc enum. Then you can just declare the @NSManaged property using the proper type instead of having to write a getter and setter. The disadvantage of this, though, is that an @objc enum only shows up as the type name if you ask for its description. To see the actual case, e.g. when logging, you would need to implement CustomStringConvertible.

There are also a lot more videos from the try! Swift Conference.

Previously: Using Core Data With Swift, Generating Core Data Swift, Modern Core Data With Swift, momdec: Core Data Model Decompiler.

2 Comments

I switch from mogenerator to Xcode generator some time ago, and the only thing I miss is the use of Set instead of NSSet for relationship.
The other big difference is that Xcode generates all properties as Swift Optional, but that a debatable choice. When creating a new managed object, all its properties are null, even if they will be required to be able to save the object, so it is never safe to assume a property can't be null.

One more thing about mogenerator. Trying to generate more swifty code look great, but it has a major drawback for Cocoa developers. Swift types are not compatible with Cocoa Binding.
While this is not an issue on iOS, it may be a big issue on macOS, where binding is still massively used for UI (especially if you want to properly support unlimited undo that force you to track all changes to the model and reflect them in the UI).

Stay up-to-date by subscribing to the Comments RSS Feed for this post.

Leave a Comment