oreilly.comSafari Books Online.Conferences.


AddThis Social Bookmark Button

Mac OS X's Preferences System (and More!)
Pages: 1, 2, 3

Saving Data

Learning CocoaLearning Cocoa
By Apple Computer, Inc.
Table of Contents
Sample Chapter
Full Description
Read Online -- Safari

Now that we have a way to load previously saved data into records, we need to implement its counterpart: a way to save the data. This is easily accomplished with the method writeToFile:atomically:. The first argument to this method is again recordsFile, and the second argument takes a BOOL value -- YES or NO.

If atomically is NO, then the data will be written directly to the file at the indicated path. If atomically is set to YES, then the method will write the contents of the message receiver to a temporary file, and only when it determines the write to be a success will the actual data file will be replaced by the temporary file. This process will prevent the data file from being corrupted in the event of any system failure or crash, so it's almost always a good idea to use YES as the atomically argument. With the following line then, we can save the contents of records to the file at recordsFile:

[records writeToFile:recordsFile atomically:YES];

Ideally we would like to save the contents of records to disk whenever they are modified. By doing this we don't have to worry about losing unsaved changes if something unexpectedly goes wrong with the application. So we would then want to invoke writeToFile:atomically: in addRecord:, deleteRecord:, and insertRecord: (if you implemented the NSTableDataSource method tableView:setObjectValue:forTableColumn:row: that I mentioned in the previous column, then you would invoke it there, too).

Rather than scattering this line around in each method that modifies records, we will create a new method in Controller called saveData, and call saveData in each of the aforementioned modifying methods. The definition of saveData is shown below:

- (void)saveData
    [records writeToFile:recordsFile atomically:YES];

Factoring out the data-saving code into saveData has the desirable benefit of making our code more maintainable. If we decide to change the way data is saved, then we only have to change it in one place.

Now all we have to do is add the line of code [self saveData] to each of the methods deleteRecord, addRecord and insertRecord right around where we do [tableView reloadData]. The method addRecord, for example, will now look like:

- (IBAction)addRecord:(id)sender
    [records addObject:[self createRecord]];
    [tableView reloadData];
    [self saveData];

With these simple pieces of code in place, your application is now capable of saving and loading the same set of data between different sessions. Now I want to say a word about property list documents, and then we'll go on to discuss Mac OS X's preferences system.

Property Lists

A property list is a type of XML (eXtensible Markup Language) that is used extensively by Mac OS X to store software configuration information such as user defaults. The contents of a property list is an XML structured textual representation of the memory data of runtime objects of classes such as NSDictionary, NSString and NSArray. Property lists are then able to preserve the structure and contents of our data structure at each level-array, dictionary and string. I'm not going to get into a discussion about XML here, because the topic is just too huge for this column. For a good summary of XML, see WC3's XML in 10 points or visit O'Reilly's

Pages: 1, 2, 3

Next Pagearrow