A blog about web development

Paul Sturgess

Core Data in RubyMotion – Defining Your Schema in Code

One of the things I love about RubyMotion is being able to build applications without having to heavily rely on XCode. I feel far more comfortable in a text editor, where I can actually see the code that makes it work.

Typically Core Data schemas are defined in XCode, with text fields and drop down options used to define the attributes, relationships etc.

Fortunately for RubyMotion developers the fantastic ruby-xcdm allows us to define our schema in code.

This may seem like more work, but one of benefits to defining the schema in code is visibility. With the schema checked into version control, it’s easy to see how it has evolved over time. Who made the changes, when they made those changes and hopefully the commit message might even say why they made those changes.

The ruby-xcdm Readme does a great job of explaining how to set it up. However, I would like to talk a bit about versioning.

If you’re coming from a Ruby on Rails background (as I was) then CoreData doesn’t really ‘do’ migrations like ActiveRecord does. I’d recommend reading up on the term ‘lightweight migration’ in the Apple docs. Essentially for simple things, like adding or removing attributes, it can do these automatically and they can be performed using ruby-xcdm.

To make this work, all you need to do is copy your schema and create a new file. Just make sure you increment the schema number and as long as you have…

task :"build:simulator" => :"schema:build"

…in your Rakefile, then the schema will be built and CoreData will perform the migration.

Ruby-xcdm can handle typical relationship types like has_many, has_one and belongs_to. It also can handle entities that inherit from a base entity - just lookup the abstract: true and parent: options.

Shameless plug

This article is part of a series of blog posts about how I built Title Challenge – A football manager game for iOS.

Others i’ve written include:

Please do check out Title Challenge on the App Store.