Magical Record: Simple Core Data


Personally I hate adding libraries by simply dragging and copying files into my code. The mixing between my own code and external libraries can quickly become confusing and difficult to disentangle. My favourite way to add libraries is Cocoa Pods. It’s very easy to install and even easier to use. I won’t get into the details as it’s well documented (on its own site as well as in many other tutorials). If you really don’t want to use pods then standard instructions for installing MagicalRecord are also well documented on its own site and elsewhere.

To install using Cocoa Pods, navigate to your project files using the terminal and type

Type the following into the podfile and save.

Next, run the following command in the same folder

Cocoa Pod will do all the work and tell you ‘MagicNotes.xcworkspace’ so close down your xcodeproj file and open the workspace. Congratulations. MagicalRecord is now installed.

To save repeating yourself you can add the line #import <CoreDate+MagicalRecord.h> to AppName-prefix.pch (under Supporting Files). To turn off MagicalRecords logging, add #define MR_ENABLE_ACTIVE_RECORD_LOGGING 0 to Pods-prefix.pch. 

Create the Core Data Files

Creating the Core Data files is no different under MagicalRecord. First create a data model (File>New>File>Core Data>Data Model); its name isn’t important for a simple app such as this as we will only be using a single data model.

Create a new data model

For this simple app we need one entity called Note. It has 3 attributes: title, date and content.

Data model Select the entity title and create an entity subclass (Editor>Create NSManagedObject Subclass…). Finally, I would recommend creating categories for any NSManagedObject subclasses. This allows you to add code to the subclass without worrying about it being wiped if you need to regenerate the base subclass files. Your project should now look something like this (I’ve added folders to make the structure clearer).

MagicNoteProject layout

To your new category you need to add one method that’ll come in handy later. This method just gives an easy and consistent way to return the date formatted as a NSString:

Create the Database

If you’ve ever used Core Data, then you’ll understand just how much code is needed just to initialise a database. With MagicalRecord it is a one line process. Just add the following two lines to your AppDelegate file to initiate the database on startup, and clean up when the app exits.

 Fetching Results

Now we have a database we need to set up our NSFetchedResultsController in order to query it. Again, this is a simple one line process. Go to NoteListViewController.m and import the Note NSManagedObject subclass category we made earlier.

Then make the following change to the setupFetchedResultsController method.

Now setup the table to display the results by making the following change to the tableView:cellForRowAtIndexPath: method

Testing Our Code

At this point you can run the app, and once again see a blank table. So lets just check we haven’t made any mistakes by quickly adding a test method to add records to the table. In NoteListViewController.m add the following method, and call it in viewDidLoad.

This simply creates two new Note entities with different values (one with todays date, and one two days ago), and saves them to the current context. Now you should gain two new table rows every time you run the app.

New table rows

This is the basis of our test app, and in the next tutorial I will show you how to add new rows the right way, and also how to edit table content.

Series Navigation<< UITableViewController and NSFetchedResultsControllerTableViewController: Detail View >>

Leave a Reply

  • (will not be published)

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">