Skip to content

Setup your entities using LFSCoreData patterns

David Cortés edited this page Feb 23, 2014 · 1 revision

Install mogenerator

brew install mogenerator

Create models using mogenerator

Create Core Data model file for your project:

Setup your model to use the same name in class field as in name field.

Execute mogenerator in your models folder, like this:

$~ mogenerator --template-var arc=true --model YourProject.xcdatamodeld/YourProject.xcdatamodel

NOTE: You have to execute this command every time you make a change in your Data Model and the command is not easy to remember, so what we do is use an alias called mog declared this way on the bash profile.

alias mog='mogenerator --template-var arc=true --model'

The next time you do an update you only type:

$~ mog YourProject.xcdatamodeld/YourProject.xcdatamodel

Before that, _YourModel.{h,m} and YourModel.{h,m} files will be created. You have to add this files to your project in Xcode.

Entity identifiers

Most common APIs use "id" as the entity identifier. In Core Data is not possible to have a field named "id" so LFSCoreData use a convention. For example for a entity named Tweet you should use tweetID as the entity unique identifier. This will be used by all the imports to preserve uniqueness of objects.

Custom model fields

The easiest way to work with LFSCoreData is setting up the names of the fields exactly as the source data. This is not always possible because APIs has elements inside other elements, the field name start with a capital letter, and other things. When you have this problem you should use mappingPath attribute in your xcdatamodel:

{
   "id" : 1234567890
   "VeryComplexNameForName":"Son",
   "surname":"Goku"
}