Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Beginning iOS5 Development.pdf
Скачиваний:
8
Добавлен:
09.05.2015
Размер:
15.6 Mб
Скачать

CHAPTER 13: Basic Data Persistence

473

if (sqlite3_step(stmt) != SQLITE_DONE) { NSAssert(0, @"Error updating table.");

}

sqlite3_finalize(stmt);

}

Notice that we used an assertion here to check for an error condition. We use assertions rather than exceptions or manual error checking because this condition should happen only if we, the developers, make a mistake. Using this assertion macro will help us debug our code, and it can be stripped out of our final application. If an error condition is one that a user might reasonably experience, you should probably use some other form of error checking.

NOTE: There is one condition that could cause an error to occur in the preceding SQLite code that is not a programmer error. If the device’s storage is completely full—to the extent that

SQLite can’t save its changes to the database—then an error will occur here as well. However, this condition is fairly rare, and will probably result in deeper problems for the user, outside the scope of our app’s data. Probably our app wouldn’t even launch successfully if the system were

in that state. So we’re going to just sidestep the issue entirely.

Once we’re finished with the loop, we close the database.

sqlite3_close(database);

Why don’t you compile and run the app? Enter some data, and then press the iPhone simulator’s home button. Quit the simulator (to force the app to actually quit), and then relaunch the Persistence application. That data should be right where you left it. As far as the user is concerned, there’s absolutely no difference between the various versions of this application, but each version uses a very different persistence mechanism.

Using Core Data

The final technique we’re going to demonstrate in this chapter is how to implement persistence using Apple’s Core Data framework. Core Data is a robust, full-featured persistence tool. Here, we will show you how to use Core Data to re-create the same persistence you’ve seen in our Persistence application so far.

NOTE: For more comprehensive coverage of Core Data, check out More iOS 5 Development by

Alex Horovitz and Kevin Kim (Apress, 2011). That book devotes several chapters to Core Data.

In Xcode, create a new project. This time, select the Empty Application template and click Next. Name the product Core Data Persistence, and select iPhone from the Device Family popup, but don’t click the Next button just yet. If you look just below the Device Family popup, you should see a checkbox labeled Use Core Data. There’s a certain amount of complexity involved in adding Core Data to an existing project, so Apple has

www.it-ebooks.info

474

CHAPTER 13: Basic Data Persistence

kindly provided an option with some application project templates to do much of the work for you.

Check the Use Core Data checkbox (see Figure 13–5), and then click the Next button. When prompted, choose a directory to store your project, and click Create.

Figure 13–5. Some project templates, including Empty Application, offer the option to use Core Data for persistence.

Before we move on to our code, let’s take a look at the project window, which contains some new stuff. Expand the Core Date Persistence and Supporting Files folders (see Figure 13–6).

www.it-ebooks.info

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]