Core Data by Marcus Zarra

Core Data by Marcus Zarra book cover

There are many books on general iOS development but they often only provide brief coverage of Core Data. I was therefore pleased to see that my favourite book on the topic, Core Data, Second Edition by Marcus S. Zarra (The Pragmatic Bookshelf, January 2013) has recently received a major update.

First Impressions

The first edition of Core Data by Marcus Zarra was the book I turned to when I wanted to learn Core Data. Whilst it is still a great book it was starting to get a little dated. It was also heavily biased to using Core Data on the OS X desktop platform rather than iOS. This was not such a huge problem as many of the key concepts are valid for both platforms. However the use of Core Data for iOS was restricted to a single chapter which meant that iOS specific issues and features such as NSFetchedResultsController got only a brief mention.

The second edition has been substantially updated and the change in emphasis is evident from the change in subtitles of the two editions. The first edition was subtitled Apple’s API for Persisting Data on Mac OS X where for the 2nd Edition it is Data Storage and Management for iOS, OS X and iCloud. The growing army of iOS developers has had an impact:

The largest number of Objective-C developers develop only for iOS, and therefore we will keep our focus there.

It is also worth mentioning that the book covers the latest updates to the Core Data framework as shipped with iOS 6 and OS X 10.8. In fact, Marcus goes as far as to recommend not using the newest Core Data API’s if you need backward compatibility:

However, iOS 5.0 (and Mac OS X 10.7) was a stumble, a misstep. It is far better to skip it and move on to iOS 6.0. But what about when your client/boss/customer requires you to be compatible with iOS 5.0? My advice is to use only the Core Data APIs that were available in iOS 4.x.

In Depth

The first half of the book provides an in depth coverage of the key concepts of Core Data. The example code is for an iOS application and a whole chapter is dedicated to covering NSFetchedResultsController reflecting the new focus of the book.

The next three chapters on Core Data versioning and migration, performance tuning and threading are all essential reading. Even if you consider yourself a Core Data expert there is a lot of good practical advice here. The treatment of migrations covers both lightweight and heavy migrations with sufficient detail to understand why heavy migrations can be a headache. The discussion on performance tuning has good advice about handling binary data and also on when to denormalize data. This is a good lesson to learn if you are coming to Core Data with an SQL database background. Finally the chapter on threading covers how to use Core Data in a way that is thread safe.

This expanded and updated edition now also provides a detailed discussion of using Core Data with iCloud. This covers both the traditional Core Data stack and the newer UIManagedDocument API. Marcus does mention the problems Apple has had getting this to work reliably:

Unfortunately, as has been demonstrated numerous times in the past, syncing is hard, very hard. Apple did not get it working acceptably in iOS 5.0 or OS X 10.7. It was not until iOS 6.0 and OS X 10.8 that iCloud has become truly reliable.

I still do not feel confident syncing the standard Core Data stack with iCloud even with iOS 6.0. The known issue when the user turns off iCloud is covered and a number of experienced developers are still reporting issues. Hopefully when Apple do finally fix it Marcus will update the book with the details.

The second half of the book moves to using Core Data with OS X. If you are primarily an iOS developer this can still be interesting reading. Firstly because you should realise that you already know much of what is required. Secondly you can get a deeper understanding of some key technologies (if you will excuse the pun) such as KVC and KVO when used on the desktop platform.

The last three chapters of the book are somewhat bonus material. There is a discussion on how to integrate Core Data with Spotlight and Quick Look on the desktop. A further example of using KVC to store dynamic paramaters and an “academic” investigation on remotely accessing a Core Data repository using Bonjour. Interesting reading but not essential to understanding Core Data.

Likes and Dislikes

There is not much to dislike. This is one of those books that works well both for introductory and advanced audiences. If you are new to Core Data the basics are covered to get you up to speed. On the other hand if you have been using Core Data for a while you can jump straight to the chapters on migrations, performance and threading and learn something.

I have a preference for technical books that are written by experienced practitioners with a strong point of view. I don’t want a book that simply reformats the information in Apple’s documentation. The parts of this book that I like best are when Marcus is giving us his opinion on how to get the best from Core Data and what you should avoid. Overall a great book that just got better.

Final Comments

You can buy the dead-tree version of the book from the usual places but I prefer to get the ebook version direct from the web site.