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

448

CHAPTER 13: Basic Data Persistence

Getting the tmp Directory

Getting a reference to your application’s temporary directory is even easier than getting a reference to the Documents directory. The Foundation function called NSTemporaryDirectory() will return a string containing the full path to your application’s temporary directory. To create a file name for a file that will be stored in the temporary directory, first find the temporary directory:

NSString *tempPath = NSTemporaryDirectory();

Then create a path to a file in that directory by appending a file name to that path, like this:

NSString *tempFile = [tempPath stringByAppendingPathComponent:@"tempFile.txt"];

File-Saving Strategies

All four approaches we’re going to look at in this chapter make use of the iOS file system. In the case of SQLite3, you’ll create a single SQLite3 database file and let SQLite3 worry about storing and retrieving your data. In its simplest form, Core Data takes care of all the file system management for you. With the other two persistence mechanisms—property lists and archiving—you need to put some thought into whether you are going to store your data in a single file or in multiple files.

Single-File Persistence

Using a single file for data storage is the easiest approach, and with many applications, it is a perfectly acceptable one. You start off by creating a root object, usually an NSArray or NSDictionary (your root object can also be based on a custom class when using archiving). Next, you populate your root object with all the program data that needs to be persisted. Whenever you need to save, your code rewrites the entire contents of that root object to a single file. When your application launches, it reads the entire contents of that file into memory. When it quits, it writes out the entire contents. This is the approach we’ll use in this chapter.

The downside of using a single file is that you need to load all of your application’s data into memory, and you must write all of it to the file system for even the smallest changes. But if your application isn’t likely to manage more than a few megabytes of data, this approach is probably fine, and its simplicity will certainly make your life easier.

www.it-ebooks.info

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