Skip to content
CoreData relationship example
Latest commit 1d99fe3 Nov 15, 2013 @caramdache Update README for iCloud
Failed to load latest commit information.
app iCloud support Nov 14, 2013
resources Version 1.0 Jun 9, 2012
spec Version 1.0 Jun 9, 2012
vendor Link to common TestFlight directory. Nov 2, 2012
.gitignore Updated tableview library Oct 26, 2013 Update README for iCloud Nov 15, 2013
Rakefile Update README for iCloud Nov 15, 2013
games.tmproj cleanup Sep 14, 2012

CoreData relationship sample (w/ preliminary iCloud support)

NOTE: Preliminary iCloud support has just been added.

This sample demonstrate how to use CoreData in RubyMotion without using XCode and XCode data models:

  • relationships and attributes are supported
  • default, optional, transient and indexed properties are supported for attributes
  • optional, transient, indexed, ordered, min, max and delete-rule properties are supported for relationships
  • attributes and relationships are specified declaratively and on a per object basis
  • store.rb is mostly independent from the objects
  • some CoreData helper/extension classes are provided in lib/
  • both table views with and without sections are demonstrated

Creating a Game class

A Game is identified by a name, occurs within a given year and has a timestamp. A game also has several players:

class Game < NSManagedObject

  @attributes = [
    {:name => 'name',      :type => NSStringAttributeType},
    {:name => 'timestamp', :type => NSDateAttributeType},
    {:name => 'year',      :type => NSInteger16AttributeType},

  @relationships = [
    {:name => 'players', :destination => 'Player', :inverse => 'game'},

In addition, games are grouped by year, sorted by timestamp and searched by name:

class Game < NSManagedObject
  @sortKeys = ['timestamp']
  @sectionKey = 'year'
  @searchKey = 'name'

Options are available if you wish to provide defaults for attributes, or if attributes are not optional, transient or indexed:

class Game < NSManagedObject
  @attributes = [
    {:name => 'name', :type => NSStringAttributeType, :default => '', :optional => false, :transient => false, :indexed => false},
    {:name => 'timestamp', :type => NSDateAttributeType, :default => nil, :optional => false, :transient => false, :indexed => false},
    {:name => 'year', :type => NSInteger16AttributeType, :default => 0, :optional => false, :transient => false, :indexed => false},

The same holds for relationships:

class Game < NSManagedObject
  @relationships = [
    {:name => 'players', :destination => 'Player', :inverse => 'game', :json => 'players', :optional => true, :transient => false, :indexed => false, :ordered => true, :min => 0, :max => NSIntegerMax, :del => NSCascadeDeleteRule},

Creating a Store

A Store is created simply by indicating the name of the NSManagedObject classes; in our example Game and Player:

class Store
  DB = 'store.sqlite'
  ManagedObjectClasses = [Game, Player]

Accessing a Game

The fetchedResultsController for a game is accessed as follows. You need to first reset the cache for the fetchedResultsController:


If you want instead to filter objects using a given search string, use:


Here's how to access the section name, the number of objects in the section and an object at the given indexpath:


Or it you want to use the search controller instead:


Editing a Game

A game can be edited easily; a dedicated UITableView will be created automatically to let the user edit the game's fields:

@gameController ||= UITableViewControllerForNSManagedObject.alloc.initWithStyle(UITableViewStyleGrouped)
@navGameController ||= UINavigationControllerDoneCancel.withRootViewController(@gameController, target:self, done:'doneEditing', cancel:'cancelEditing')

@gameController.object = game
@gameController.is_update = true
navigationController.presentModalViewController(@navGameController, animated:true)

From Game to JSON (and vice-versa)

A Game can then easily be output in JSON:


A Game can be easily created from a JSON string:

json = '{
    "timestamp": "5/11/2012",
    "year": 2012,
    "name": "game1"

Game.add do |game|
Something went wrong with that request. Please try again.