iOS SDK : Property Lists and Archiving - Archiving
Published 11 min readBy Programming4us Editorial Team
You can only serialize and deserialize
property list objects. Moreover, all of a property list object’s
constituent objects must also be property list objects. This limitation
hinders the usefulness of property lists. Therefore, rather than using a
property list, you can use archiving. Archiving is a more flexible
approach to persisting an object than a property list.
You create an archive using an NSKeyedArchiver. This
class persists any object that adopts the NSCoding protocol. You
reconstitute an object by using NSKeyedArchiver’s complement, the
NSKeyedUnarchiver class. In this section, you learn how to create a
class that adopts the NSCoding protocol. You then learn how to archive
and unarchive this class.
1. Protocols to Adopt
Archiving a class requires that a class adopt the
NSCoding protocol. The class should also adopt the NSCopying protocol if
you’re creating a class that adopts the NSCoding protocol.
NSCoding
Classes that adopt this protocol must implement the
encodeWithCoder: and initWithCoder: methods. The encodeWithCoder: method
encodes the object and the object’s instance variables so that they can
be archived.
-(void)encodeWithCoder:(NSCoder *)encoder
The initWithCoder: method decodes the object and the object’s instance variables.
-(id)initWithCoder:(NSCoder *)decoder
You use both methods in the example task that follows.
NSCopying
When implementing the NSCoding protocol, best
practices dictate that you also implement the NSCopying protocol.
Classes that implement the NSCopying protocol must implement the
copyWithZone method. Remember, when you set one object to another, you
are merely creating another reference to the same underlying physical
object. For instance, in the following code, both A and B are pointing
to the same Foo that was originally allocated and initialized by A.
Foo * A = [[Foo alloc] init];
Foo * B = A;
When you copy an object, you obtain a distinct
physical object, as if the object obtaining the copy actually allocated
and initialized the object.
Foo * A = [[Foo alloc] init];
Foo * B = [A copy];
The method that allows copying is the copyWithZone: method.
-(id)copyWithZone:(NSZone *)zone
You can use either this method or NSObject’s copy
method to obtain what is called a “deep copy” of an object. For more
information, refer to Apple’s “Memory Management Programming Guide for
Cocoa,” available online.
Note
2. NSKeyedArchiver and NSKeyedUnarchiver
The NSKeyedArchiver class archives objects, while the NSKeyedUnarchiver class unarchives objects.
NSKeyedArchiver
NSKeyedArchiver stores one or more objects to an
archive using the initForWritingWith MutableData method. To be archived,
an object must implement the NSCoding protocol.
After initializing the NSKeyedArchiver, encode the
objects to archive. If you wish, you can encode multiple objects using
the same archiver, provided all archived objects adopt the NSCoding
protocol. The following code snippet illustrates:
You use NSKeyedUnarchiver to unarchive an archive.
NSKeyedUnarchiver reconstitutes one or more objects from a data object
that was initialized with an archive. To be unarchived, an object must
implement the NSCoding protocol. When programming for iOS, you use the
initForReadingWithData: method.
-(id)initForReadingWithData:(NSData *)data
The steps to unarchive are similar to archiving. First, create an NSData object from the previously archived file.
After initializing the NSKeyedUnarchiver, unarchive the objects previously archived.
A * objA = [[unarchiver decodeObjectForKey:@"a"] retain];
B * objB = [[unarchiver decodeObjectForKey:@"b"] retain];
C * objC = [[unarchiver decodeObjectForKey:@"c"] retain];
[unarchiver finishDecoding];
[unarchiver release];
Listing 1. Foo.h
Listing 2. Foo.m
Listing 3. EncodingAppDelegate.h
Listing 4. EncodingAppDelegate.m
Listing 5. Bar.h
Listing 6. Bar.m
Listing 7. EncodingAppDelegate.h
Listing 8. EncodingAppDelegate.m
When
the application starts, it loads the archive file into a data object. If
the data object is null, the file doesn’t exist. If the file does
exist, the data is unarchived. When the application terminates, it
archives Foo. Because Bar contains constituent Foo objects in an array,
it also archives those objects. The key for the archived Foo is “myfoo,”
and “mybar” for the archived Bar object. Both Foo and Bar implement the
NSCoding protocol. This allows them to be archived. Notice that Bar
contains an NSMutableArray of Foo objects. Because NSMutableArray adopts
the NSCoding protocol, NSMutableArray can be encoded and decoded.
Moreover, the NSMutableArray knows to encode or decode its constituent
elements.
Now examine Bar’s copyWithZone method. Because Bar
contains an NSMutableArray of Foo objects, when copying a Bar you must
also copy the Bar’s Foo array. But you cannot just set the new Bar’s
array to the old Bar’s array, as the new Bar’s array will simply be a
pointer to the old Bar’s array. Instead you must create a new
NSMutableArray and initialize the new array with the old array, being
certain to specify copyItems as YES. By taking this step, the new Bar
obtains a deep copy of the old Bar’s array of Foo objects.
Note
For more information on archiving, refer to “Apple’s Archives and Serializations Programming Guide for Cocoa.”
3. Multitasking and Saving Application State
In
versions of the iOS prior to 4, when the user pressed the Home button,
your application was terminated. Now the default behavior is to leave
your application in memory and just suspend it. Then if the user wants
to return to it later, it can launch instantly and they’re exactly where
they left off. We actually had to turn this behavior off in the
encoding task so that it would terminate and save our test data. For
simple apps that launch quickly and aren’t likely to be returned to over
and over, it makes sense to set that flag and not worry about coding
for application suspension. However, for more complex applications your
users will really appreciate being able to switch to another app and
then instantly return to yours, continuing right where they left off.
So, what does this have to do with archiving? If all
the iOS ever did was temporarily freeze your app in memory and then
continue it later, there wouldn’t be any need for persistence. But, if
iOS runs low on memory or the user doesn’t return to your application
for a while, then it can be purged from memory without warning. This
creates unpredictable behavior for your users. Sometimes when they
switch to another app and then return to yours, everything is exactly as
they left it (e.g., they’re in the middle of a gaming level or tunneled
deeply down into some nested UITableViews). Other times, when they
return to your app it seems to be starting up fresh (because it is—iOS
had to purge it from memory). The recommended way to deal with this
inconsistency is to always save away enough state information when your
application is suspended so that you can restore it to the exact same
context if your application is purged from memory before the user can
return to it. That’s where archiving comes in.
The state information you’ll need to save is going to
be different for every application, but the easiest way to save that
information is going to be a set of objects serialized and archived to a
file in the application’s Documents directory. For a game you might
need to serialize a number indicating the level they were on along with a
hierarchy of objects that encode the state of enemies, puzzles, etc.,
on that level. For a reference application that browses hierarchical
data, it might be saving a trail of which item they chose at each level
and where they were scrolled to on the current view. In either case, you
will likely want to implement the NSCoding protocol for a selection of
objects, so that you can archive them when your application is
suspended. Implement the applicationDidEnterBackground method in your
AppDelegate class and save your state information there as well as
freeing up any resources you don’t need.