This book is a practical guide to help you in developing Data-Driven iPhone applications using Core Data. The tremendous success of iPhone has increased the demand of mobile applications. Besides the Game-based applications, there is a huge market for the data-driven mobile applications too. The focus of this book is to make you understand how the Core Data, Apple's persistence framework, is used for developing data-driven mobile applications.
This book assumes that you have a basic understanding of the iPhone SDK and you also know the basics of iPhone SDK programming.
To better understand the concept of Core Data, you should:
Even if you're not aware of these two concepts, Chapter 3, Understanding Objective-C Protocol and Table View and Chapter 4, Designing a Data Model and Building Data Objects for Customers of the book are focused to get you acquainted with them. That is why the two chapters are self-contained and each chapter presents an individual application.
The iPhone as we all know is an integrated cellular telephone and media player developed and marketed by Apple. It has become very popular in the past few years because of its amazing features. Looking at its huge number of users, developers around the world are attracted to develop 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 Object Framework (EOF) in 1994.
EOF is an object-relational mapping (ORM) framework that provides a mechanism for accessing the data as an object-oriented class structure. It is well-designed and encourages Model View Controller (MVC) design patterns. It also simplifies the tedious job of creating an application's data model. EOF is not just a framework, it is also a tool that helps in creating the application's data model visually — the task that was previously done by creating Objective-C classes. Besides this, the framework handles all the work involved 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 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 that was specifically designed to provide local data storage for Cocoa applications. The modeling functionality of Core Data is integrated right into XCode, so there's no need to switch back and forth between the IDE and modeler. With interface builder, it allows developers to quickly create a user interface (known as the views of the application in MVC terminology) without writing a single line of code. It is also the most effective solution to data persistence and 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 to a storage medium such as an XML file or SQLite database.
Now the question arises, what is Xcode and why we are using it for developing Core Data applications?
Xcode is Apple's most comprehensive Software Development Kit (SDK), and it provides an environment for developing the applications for iPhone. It is a highly customizable integrated development environment (IDE) that includes compilers and applications, together with an extensive set of programming libraries and interfaces. It is a powerful source editor and a graphic debugger too. While developing applications with XCode, it gives us an option to enable a checkbox for enabling Core Data support. On selecting the checkbox, Xcode automatically creates code for us that make the task of developing core data applications quite easy.
You'll find chapter-wise code bundle in the ZIP file. The book is so organized that it guides you to develop a data-drive application step-by-step. That is, by the end of the book, you'll be having a complete data-driven running application with you. In case, you want to run the end product directly, follow the below given steps:
1. Unzip the source bundle of the last chapter, Chapter 11, Displaying the Products for Sale and Updating the Stock on your local Mac.
2. Open Xcode, go to File | Open from the menu, and browse to the unzipped bundle of Chapter 11, Displaying the Products for Sale and Updating the Stock. In the
probfolder, select the
prob.xcodeprojfile followed by clicking on the Open button.
3. Select the Build and Run icon from the Xcode project window to run the application. You'll get the main view of the application as shown in the following image. But the application is not yet ready to run until we define the photos of the master products (products that we are going to sell through application).
4. To define photos of the master products in iPhone Simulator, go to Home and then click on the Photos icon (refer to the given image (a)).
5. We get the Albums page as shown in image (b). Because we have not created any photo album yet, the figure displays the message, No Photos.
6. Drag the first image,
IMG_0000.JPGprovided in the code bundle of Chapter 11, Displaying the Products for Sale and Updating the Stock onto the simulator screen. Tap on the image and hold down the mouse on the image until the popover comes up, as shown in image (c). Click on the Save Image button to save the image.
7. Repeat the procedure for the other three images (IMG_0001.JPG, IMG_0002.JPG, IMG_0003.JPG). After saving the four images, the simulator will display the images as shown in image (a).
8. On clicking back to Photos, we find that an album Saved Photos appears with one of the images considered as the icon of the photo album (image (b)). The number (4) in parenthesis represents that there are four images in this photo album.
9. Now, our application is completely ready for execution. For any guidance regarding operating the application, refer to the An application output sample section in Chapter 2, Understanding Core Data.