Core Data in Objective-C, Third Edition
Data Storage and Management for iOS and OS X
by Marcus S. Zarra
Version: P2.0 (July 2016)
Copyright 2016 The Pragmatic Programmers, LLC. This book is licensed to the individual who purchased it. We don't copy-protect it because that would limit your ability to use it for your own purposes. Please don't break this trustyou can use this across all of your devices but please do not share this copy with other members of your team, with friends, or via file sharing services. Thanks.
Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and The Pragmatic Programmers, LLC was aware of a trademark claim, the designations have been printed in initial capital letters or in all capitals. The Pragmatic Starter Kit, The Pragmatic Programmer, Pragmatic Programming, Pragmatic Bookshelf and the linking g device are trademarks of The Pragmatic Programmers, LLC.
Every precaution was taken in the preparation of this book. However, the publisher assumes no responsibility for errors or omissions, or for damages that may result from the use of information (including program listings) contained herein.
About the Pragmatic Bookshelf
The Pragmatic Bookshelf is an agile publishing company. Were here because we want to improve the lives of developers. We do this by creating timely, practical titles, written by programmers for programmers.
Our Pragmatic courses, workshops, and other products can help you and your team create better software and have more fun. For more information, as well as the latest Pragmatic titles, please visit us at http://pragprog.com.
Our ebooks do not contain any Digital Restrictions Management, and have always been DRM-free. We pioneered the beta book concept, where you can purchase and read a book while its still being written, and provide feedback to the author to help make a better book for everyone. Free resources for all purchasers include source code downloads (if applicable), errata and discussion forums, all available on the book's home page at pragprog.com. Were here to make your life easier.
New Book Announcements
Want to keep up on our latest titles and announcements, and occasional special offers? Just create an account on pragprog.com (an email address and a password is all it takes) and select the checkbox to receive newsletters. You can also follow us on twitter as @pragprog.
About Ebook Formats
If you buy directly from pragprog.com, you get ebooks in all available formats for one price. You can synch your ebooks amongst all your devices (including iPhone/iPad, Android, laptops, etc.) via Dropbox. You get free updates for the life of the edition. And, of course, you can always come back and re-download your books when needed. Ebooks bought from the Amazon Kindle store are subject to Amazon's polices. Limitations in Amazon's file format may cause ebooks to display differently on different devices. For more information, please see our FAQ at pragprog.com/frequently-asked-questions/ebooks. To learn more about this book and access the free resources, go to https://pragprog.com/book/mzcd3, the book's homepage.
Thanks for your continued support,
Dave Thomas and Andy Hunt
The Pragmatic Programmers
The team that produced this book includes: Jacquelyn Carter (editor) Potomac Indexing, LLC (indexer) Liz Welch (copyeditor) Gilson Graphics (layout) Janet Furlow (producer)
For customer support, please contact .
For international rights, please contact .
Table of Contents
Copyright 2016, The Pragmatic Bookshelf.
Early Praise for Core Data in Objective-C, Third Edition
Marcuss books have long been my go-to recommendation for people who want to learn about Core Data. With new information for beginners and experts alike, they are a great insight into how you should be using Core Data and the features that it offers.
Daniel Tomlinson |
Senior software engineer, Rocket Apps Ltd. |
Core Data is a very powerful resource for any developer interested in iOS and OS X application development. This book does an excellent job of explaining the details of Core Data, a topic that is otherwise considered quite hard to grasp. A must-read book for anyone developing data-intensive applications.
Ramaninder Singh Jhajj |
Area Services and Development Know-Center, Austria |
Marcus has a profound understanding of Core Data, and this book describes not only the basics of working with Core Data, but also how to do so in a performant manner, with explanations and examples. This is an indispensable reference, alongside Apples documentation, and one that will quickly make Core Data your go-to persistence mechanism. Youll be able to deal with anything that comes up with confidence.
Abizer Nasir |
Lead developer, Jungle Candy Software Ltd. |
Introduction
It is amazing to realize that as of the writing of this book, Core Data is over 12 years old. Thats an amazing number when you consider that its more powerful and more popular today than it has ever been. For a framework, thats pretty spectacular.
Is This Book for You?
If you plan on writing an application that saves data to disk, you should take a very long look at Core Data. Whether youre focusing on the desktop or the iPhone, Core Data is the most efficient solution to data persistence.
A good way to confirm that you know enough Cocoa to benefit from this book is to take a look at Chapter 1, . You may find it dense, but every step should be familiar to you.
What Is Core Data?
In the simplest terms, Core Data is an object graph that can be persisted to disk. But just like describing a man as a bag of mostly water, that description hardly does Core Data justice. If youve worked with Interface Builder (specifically on OS X), you know that it effectively removes a third of the coding from the Model-View-Controller (MVC) design pattern. With Interface Builder, developers dont need to spend countless hours writing and rewriting their user interface to make sure that its pixel perfect. Instead, they simply drag and drop the elements in the IDE, bind them together, and call it done.
Of course, the problem with Interface Builder is that we still need to code the other two parts! Both the controller and the model need to be developed in code and made to work with the interface we just designed. Thats where Core Data comes in. In a nutshell, Core Data deals with a third of that MVC design: Core Data is the model.
Its a common misconception that Core Data is a database API for Cocoa that allows a Cocoa application to store its data in a database. Although that description is factually accurate, Core Data does a lot more. It serves as the entire model layer. Its not just the persistence on disk; its also all the objects in memory that we normally consider to be data objects. If you have experience working with Java, C#, or some other object-oriented language, you know that the data objects take a lot of time to write and that theyre generally very repetitive in nature. Core Data eliminates most, if not all, of that boilerplate code for us and lets us focus on the business logic, or the controller layer, of our application. It does this with an interface thats as easy to use as Interface Builder.