www.robkerr.com
www.robkerr.com

mobile development, cloud computing and building great software

Rob Kerr
Author

Addicted to coding since writing my first programs for the Commodore computers in the 1980s. Currently working as an independent contractor focused on native iOS development.

Share


Tags


Twitter


www.robkerr.com

Big Improvements in iOS10 Swift3 CoreData

Rob KerrRob Kerr

Having a built-in relational database in iOS via CoreData is a pretty nice feature of the mobile OS. I've always liked using it, but like many developers I've never appreciated the volume of boilerplate code to create and maintain just to setup the basic scaffolding in a new project. As much as I appreciate CoreData being totally a "built in" database, the fit and finish of the API and integration just lacked polish and ease of use for Swift development.

Well with iOS 10--I have to say "not anymore". Earlier this week I setup the my first project targeting iOS 10 with CoreData, and it was just a dream to work with. With a little data model definition and a few lines of initialization in AppDelegate, I had a full database defined and running. Love it.

This isn't a tutorial on how to setup a new project with CoreData in iOS 10, but I'll note a few of the changes that make the process more streamlined.

Source of Example

The project discussion below is from a an iOS 10 upgrade of one of my demo apps in the app store. The code isn't exactly from the code base, but is excerpted from it.

Terminals

Data Model Definition -- mostly the same

Defining the data model in XCode is very much like before. If you use CoreData, all skills transfer. Create entities and attributes, etc.

The significant difference is to pay attention to the Codegen drop-down, which flags to XCode whether to create a class, a class extension, or to do nothing with your entities. In the screen grab below, I've selected Class Definition, telling XCode I want it to generate a full Class named Terminal which will be available to the project.

Codegen Setting

When the project builds, XCode generates a class (and extension) behind the scenes to provide the normal set of Manged Object functionality CoreData expects. Did you notice--no need to generate the model manually, and remember to regenerate it manually with each attribute change! Stellar!

AppDelegate Scaffolding

Also streamlined is the amount of scaffolding work in AppDelegate. This is reduced from dozens of lines of boilerplate code to create/maintain, down to just a very few.

Sometimes you need to hand tune that scaffolding code for performance etc. This is still possible, since CoreData is back there and properties and methods are accessible. But now is more by exception rather than a requirement even for simple programs. Easy on-ramp, then add complexity as needed.

In this snippet I've eliminated code not relevant to CoreData, and as you can see, the setup required is minimal:

func application(_ application: UIApplication,   didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    coreDataStack = NSPersistentContainer(name: "Terminals")
    coreDataStack.loadPersistentStores { (description, error) in
        if let error = error {
            // Handle error appropriately
        }
    }
    return true
}

That's pretty much done. All that extra code in AppDelegate from previous versions is just gone! Again, there's tweaking and tuning depending on the requirements of the app, but a minimal CoreData app now requires so much less code to get up and running!

Accessing data in the database

Now that the database is up and running, how to insert a record? Let's look at the code. Again, the code is kept minimal for clarity.

func insertRow(context : NSManagedObjectContext, name : String, city : String, lat : Double, lon : Double) {

    do {
        let terminal = Terminal(context: context)

        terminal.name = name
        terminal.city = city
        terminal.lat = lat
        terminal.lon = lon

        try context.save()
    } catch {
        // Handle error appropriately 
    }
}

Wow, pretty straightforward!

While this isn't a complete example of writing an iOS 10 CoreData app from end-to-end, I hope it illustrates just how much more straightforward this is for the developer.

Rob Kerr
Author

Rob Kerr

Addicted to coding since writing my first programs for the Commodore computers in the 1980s. Currently working as an independent contractor focused on native iOS development.

Comments