FCModel and Current Thoughts on Core Data

Marco Arment’s Core Data alternative that sits on top of SQLite and FMDB is on GitHub and looks excellent. Core Data has generally worked well enough for me, but maybe not so well that I’m not interested in alternatives. What he’s done is make something simple with a couple of great features built-in:

  • Multi-threading support: Database operations happen concurrently via a serial queue, so you shouldn’t stomp all over yourself. I assume if you’re accessing your model objects in your own threads the normal rules apply.
  • Active record style access: You only need to worry about talking to your model objects and classes, which is simpler in most cases than the way Core Data does it.
  • Simple schema migration: You can version your database and then receive a callback to update your schema as needed.

The whole thing is pretty similar to what Brent Simmons describes in his objc.io article about using SQLite instead of Core Data. Brent raises a couple of reasons for wanting to do something a bit different in his article. Mostly it comes down to hitting performance walls and wanting something that’s just a bit simpler and less general. The truth is that because Core Data is a general solution which completely abstracts you away from the idea of using a database, I’m not sure there’s anyway that it couldn’t be a bit complex in places and that there wouldn’t be walls to bump up against. In my experience with Core Data everything works great except when it doesn’t, and because you’re so abstracted away from the implementation detail of it using a SQLite store, when you do hit those walls, you hit them hard. Some things I’ve run up against with Core Data are:

  1. Threading still sucks, even if it has gotten better.
  2. Tries to solve for edge cases I’m not sure anyone has and is overly complex because of it.
  3. Having to pass around an NSManagedObjectContext to do just about anything.
  4. When you have problems, it’s not always easy to find documentation that explains what’s going on.

All of that being said, I still think that Core Data does do a lot for you. The first three issues I raise are mostly mitigated by having good code hygiene and also by using MagicalRecord. Taking the time to read the documentation and also Marcus Zarra’s book on the topic (before you start using MagicalRecord), would probably solve a lot of problems for people as well.

As far as performance goes, it wasn’t easy to get there, but Pinbook imports bookmark collections in the range of 20,000+ in a few seconds on an iPhone 4S, so it is possible to get very high performance when using Core Data. I can’t imagine there’s too many iOS apps out there that have those kinds of performance requirements.

There’s also some specific things that Core Data really makes a lot easier. For example, it’s batching and faulting mechanisms work really well. One thing that I did not have to think about very much when developing an app that might need to display 50,000 items in a table view was managing memory for those items. I’m sure I could have come up with a way to handle that using a custom solution, and it wouldn’t have even been that hard, but with Core Data it was practically free. I’m also hearing from friends that Core Data sync might be finally working the way it should in iOS 7, and if that’s true, and I write an app which really just needs to make sure a users data remains in place between iPhone, iPad and Mac versions, would be awesome, although I’m not holding my breath until I have time to play with it myself.

No general solution will ever be perfect fit for every case. In those cases writing a custom solution is nothing to fear. If you’re writing a Mac or iOS app, however, Core Data probably is the right fit most of the time, and you should really consider what you’re doing when it’s not.

360iDev September 8-11 in Lone Tree Colorado

I'm excited to be given the opportunity to speak at 360iDev again in 2013. As usual, it's a great lineup this year, and there's a few talks this year that I'm really excited about. In particular Brent's SQLite and Justin's Core Image talk are two that I'll definitely be at. I'll be closing out the first day with a general session talk called App Making for Deadites about what Ash's journey in The Evil Dead movies can teach us about making great apps 1. I'll also be on a panel with my friends Matthew Henderson and Samuel Goodwin in the same room Tuesday morning.

360iDev will be held in Lone Tree Colorado September 8-11, 2013. You can buy your ticket now, or check out the schedule.

  1. I'm really hoping that I haven't grossly overestimated how many people have seen those movies. 

Nicer Segue Preparation With Storyboards

When we started developing the iOS app for Braid, a decision I made early on was to use Storyboards. If you’re not familiar, storyboards are a way to create iOS user interfaces visually and draw connections between the different screens in your app. In storyboard-speak, each screen is referred to as a “scene,” and the transitions between each scene is called a “segue.” Storyboards are a fantastic feature for a couple of reasons, including that when using storyboards you can set up static and dynamic table views visually, drag and drop container view controllers and get a visual picture of what you’re entire app looks like.

One part of coding with storyboards has always bothered me though — the strange way that Apple’s example code shows how to set up a view controller before a transition (setting a delegate, detail object, etc):


The string comparison felt a little gross to me, and if you have a lot of different segues, the if/else in this method could get long pretty fast, even if you’re breaking out what happens in each condition into it’s own method. If you do end up writing a method for each segue, all of those -[prepareForSomethingWithSegue:sender] methods could get repetitive pretty fast.

A nicer way is to use a block for each segue you need to prepare and to encapsulate that into it’s own class, which I did. The class is called BRLSeguePreparationController and makes it very simple to deal with preparing for segue’s in this way.

  1. Create a BRLSeguePreparationBlock for each segue you need to prepare for.
  2. Set them using -[BRLSegueController setPreparationBlock:forSegueIdentifier:]
  3. Call -[BRLSeguePreparationController prepareSegue:identifier:] from your view controllers prepareForSegue:sender: method.

Here’s an example:


I’ve given the class it’s own GitHub repo and an MIT license, so use it in your own projects. If you make any improvements, make sure you add a pull request on GitHub. If you find the class useful, please check out my companies website to find out what we’re working on, and download our Passbook pass from there to keep up to date when our app comes out.

Jumping to Protocol Definitions With Pragma Mark

Using pragma marks to organize source files is one sign that the person who wrote the class put a little bit of care into what they’re doing, but they can actually be more useful than just a way of breaking up an implementation file.

My favorite trick is to use how I name my pragmas to jump to protocol definitions more quickly. I always use a pragma mark before the implementation of a protocol in my source, and in most of the code I’ve other people write, they do something like this:

#pragma mark - Table view data source

A better way is to use the actual name of the protocol you’re implementing instead, like this:

#pragma mark - UITableViewDataSource

Now, if you command+click on that Xcode will jump right to the protocol definition, and option+double-click will take you straight to it’s documentation.