www.it-ebooks.info Core Data iOS Essentials A fast-paced, example-driven guide to data-driven iPhone, iPad, and iPod Touch applications B.M. Harwani BIRMINGHAM - MUMBAI www.it-ebooks.info Core Data iOS Essentials Copyright © 2011 Packt Publishing All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews. Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book. Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information. First published: April 2011 Production Reference: 1180411 Published by Packt Publishing Ltd. 32 Lincoln Road Olton Birmingham, B27 6PA, UK. ISBN 978-1-849690-94-2 www.packtpub.com Cover Image by Asher (a.wishkerman@mpic.de) www.it-ebooks.info Credits Author B.M. Harwani Reviewers Fred McCann Muhammad Adil Acquisition Editor David Barnes Development Editor Chris Rodrigues Technical Editor Aditi Suvarna Copy Editor Laxmi Subramanian Indexer Monica Ajmera Mehta Rekha Nair Editorial Team Leader Aditya Belpathak Vinodhan Nair Project Team Leader Lata Basantani Project Coordinator Vishal Bodwani Proofreader Aaron Nash Graphics Geetanjali Sawant Production Coordinator Alwin Roy Cover Work Alwin Roy www.it-ebooks.info About the Author B.M. Harwani is the founder and owner of Microchip Computer Education (MCE), based in Ajmer, India that provides computer education in all programming and web developing platforms. He graduated with a B.E. in computer engineering from the University of Pune, and also has a 'C' Level (Master's diploma in computer technology) from DOEACC, Government Of India. Having been involved in the teaching eld for over 16 years, he has developed the art of explaining even the most complicated topics in a straightforward and easily understandable fashion. He has written several books on various subjects that include JSP, JSF, EJB, PHP, .NET, Joomla, jQuery, and Smartphones. He also writes articles on a variety of computer subjects, which can be seen on a number of websites. To know more, visit his blog, http://bmharwani.com/blog. The list of books written by B.M. Harwani are Programming & Problem Solving through C (BPB, 2004), Learn Tally in Just Three Weeks (Pragya, 2005), Data Structures and Algorithms through C (CBC, 2006), Master Unix Shell Programming (CBC, 2006), Business Systems (CBC, 2006), Practical Java Projects (Shroff, 2007), Practical Web Services (Shroff, 2007), Java for Professionals (Shroff, 2008), C++ for Beginners (Shroff, 2009), Practical ASP.NET 3.5 Projects (Shroff, 2009), Java Server Faces—A Practical Approach for Beginners (PHI Learning, 2009), Practical JSF Project using NetBeans (PHI Learning, 2009), Foundation Joomla (Friends of ED, 2009), Practical EJB Projects (Shroff, 2009), Data Structures and Algorithms in C++ (Dreamtech Press, 2010), Developing Web Applications in PHP and AJAX (Tata McGraw Hill, 2010), and jQuery Recipes (Apress, 2010). www.it-ebooks.info Acknowledgement I owe a debt of gratitude to David Barnes, the Senior Acquisition Editor at Packt Publishing for his initial acceptance and giving me an opportunity to create this work. I am highly grateful to the whole team at Packt Publishing for their constant cooperation and contribution to create this book. My gratitude to Chris Rodrigues, who as a Development Editor offered a signicant amount of feedback that helped to improve the chapters. He played a vital role in improving the structure and the quality of information. I must thank Muhammad Adil, the Technical Reviewer for his excellent, detailed reviewing of the work and the many helpful comments and suggestions he made. Special thanks to Aditi Suvarna, the Technical Editor for rst class structural and language editing. I appreciate her efforts in enhancing the contents of the book and giving it a polished look. I also thank Alwin Roy, the Production Coordinator for doing excellent formatting and making the book dramatically better. A big and ongoing thanks to Vishal Bodwani, the Project Coordinator for doing a great job and sincere efforts by the whole team to get the book published on time. A great big thank you to the editorial and production staff and the entire team at Packt, who worked tirelessly to produce this book. I really enjoyed working with each one of you. I am also thankful to my family—my small world; Anushka (my wife) and my two little darlings, Chirag and Naman for allowing me to work on the book even during the time that I was supposed to spend with them. I should not forget to thank my dear students who have been a good teacher for me as they make me understand what basic problems they do face in a subject and enable me to directly hit at those topics. It is because of the endless interesting queries of my students that help me in writing the books with a practical approach. www.it-ebooks.info About the Reviewers Fred McCann is a co-founder of Zumisoft, an independent Mac software company. He started writing code at the tender age of eight on a TI-99a home computer and has since moved on to newer systems. His interests, outside of programming, include Judo, Yoga, Hiking, Kites, Amateur Robotics, Politics, and Meditation. His websites include http://www.duckrowing.com/ and http://www.fredmccann.com/Home.html. Muhammad Adil graduated from the National University of Computer and Emerging Sciences in Pakistan, with a Bachelor's degree in Computer Science. He was a very active Freelance Software developer during his university years. So, after graduating, he kept working as a Freelance developer and has been working as an iOS developer for the past one and a half years. www.it-ebooks.info www.PacktPub.com Support les, eBooks, discount offers, and more You might want to visit www.PacktPub.com for support les and downloads related to your book. Did you know that Packt offers eBook versions of every book published, with PDF and ePub les available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at service@packtpub.com for more details. At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters and receive exclusive discounts and offers on Packt books and eBooks. http://PacktLib.PacktPub.com Do you need instant solutions to your IT questions? PacktLib is Packt's online digital book library. Here, you can access, read, and search across Packt's entire library of books. Why Subscribe? • Fully searchable across every book published by Packt • Copy and paste, print and bookmark content • On demand and accessible via web browser Free Access for Packt account holders If you have an account with Packt at www.PacktPub.com, you can use this to access PacktLib today and view nine entirely free books. Simply use your login credentials for immediate access. www.it-ebooks.info www.it-ebooks.info This book is dedicated to my mother Mrs. Nita Harwani and Mark Zuckerberg. My mother is next to God for me and whatever I am today is because of the moral values taught by her. It is because of Mark Zuckerberg's amazing development; Facebook that I could meet my school and Engineering college friends after a long break of 18 years. www.it-ebooks.info [...]... 2 • External data repositories: Core Data supports storing objects in external data repositories in different formats Data Model Core Data describes the data in terms of a data model A data model is used to define the structure of the data in terms of entities, properties, and their relationships Entities Because Core Data maintains data in terms of objects, an entity is an individual data object to... business logic of our application www.it-ebooks.info Understanding Core Data Besides this, Core Data also provides several features that are required in data manipulation, which includes filtering data, querying data, sorting data, establishing relationships with other data, and persisting data in different repositories Core Data features The Core Data framework provides lots of features that include the following:... Prerequisite 7 A brief history 8 Enterprise Object Framework (EOF) 8 Core Data 8 Why use Xcode? 9 Source code 9 Shall we begin? 12 Chapter 2: Understanding Core Data 13 Core Data 13 Core Data features 14 Data Model 15 Entities 15 Properties 15 Attributes 16 Relationships 16 Inverse relationship 17 Model View Controller (MVC) 18 Core Data API 19 Persistent Store 21 Persistent Store Coordinator 22 FetchRequest 22... in persisting the data to a SQL database, flat file, or any other data store Based on object-oriented architecture, EOF is very flexible to use too The roots of the Core Data framework come from the Enterprise Objects Framework (EOF) Core Data Core Data is part of the Cocoa API in Mac OS X first introduced with Mac OS X 10.4 Tiger and for the iOS with iPhone SDK 3.0 It is a powerful data model framework... history of the Core Data and a small introduction to EOF and Xcode In Chapter 2, Understanding Core Data, you will have an introduction to the Core Data framework and its features You will learn about the data model and how it defines the structure of data in terms of entities, properties, and their relationships Also, you will get a brief idea of Model View Controller (MVC), the Core Data API and its... a Sales Record Keeping System application using Core Data through a step-by-step approach So, this chapter will give us an introduction to the following topics: • Core Data framework and its features • The data model and how it defines the structure of data in terms of entities, properties, and their relationships • Model-View-Controller (MVC) • Core Data API and its main components • An overview of... views are selected Core Data Core Data is Apple's persistence framework, which is used to persist—store our application's data in a persistent store, which may be memory or a flat file database It helps us represent our data model in terms of an object graph, establish relationships among objects, and it can also store object graphs on the disk It also allows us to use the entities of our data model in... applications for this unique device Developers realized that besides games, there is a huge market of data applications for iPhone device The attraction of creating data applications for iPhone device resulted into development of the Core Data framework But the question is where did Core Data come from? Core Data was first developed at NeXT Computer as the DBKit framework in 1992, which then became the Enterprise... a managed object are not loaded from the data store and the property values are accessed by the application In such situations, faulting occurs and the data is retrieved from the store automatically • Efficient database retrievals: Core Data queries are optimized for this, though the execution of query is dependent on the data store • Multi-threading: Core Data supports multi-threading in an application,... allows us to persist our data to any number of different storage mediums, which includes storing data as XML, in binary files, or in an embedded SQLite database The data modeling tool of Xcode allows us to define our application's data model graphically, which can be easily accessed through code Instances of the entities defined in the data model are then managed by the Core Data framework and stored . (EOF) 8 Core Data 8 Why use Xcode? 9 Source code 9 Shall we begin? 12 Chapter 2: Understanding Core Data 13 Core Data 13 Core Data features 14 Data Model. history of the Core Data and a small introduction to EOF and Xcode. In Chapter 2, Understanding Core Data, you will have an introduction to the Core Data framework