Archiving custom objects in other applications – Apple WebObjects 3.5 User Manual
Page 132

Chapter 7
Managing State
132
•
During unarchiving, an EOEditingContext can recreate individual objects
in the graph only as they are needed by the application. This approach can
significantly improve an application’s perceived performance.
An enterprise object (like any other object that uses the OpenStep archiving
scheme) makes itself available for archiving by declaring that it conforms to the
NSCoding protocol and by implementing the protocol’s two methods,
encodeWithCoder:
and
initWithCoder:
. It implements these methods like this:
// WebScript example
- encodeWithCoder:(NSCoder *)aCoder {
[EOEditingContext encodeObject:self withCoder:aCoder];
}
- initWithCoder:(NSCoder *)aDecoder {
[EOEditingContext initObject:self withCoder:aDecoder];
return self;
}
Even though the Java packages provide a different archiving mechanism, your
Java classes should use the Foundation archiving mechanism. In Java, the
NSCoding protocol is called the Coding interface, and it declares only one
method,
encodeWithCoder
. If your class conforms to the Coding interface, it should
also implement a constructor that takes a Coder object as an argument. (This is
the equivalent of the
initWithCoder:
method.)
// Java example
public void encodeWithCoder(Coder aCoder) {
EditingContext.encodeObjectWithCoder(this, aCoder);
}
public MyClass(Coder aDecoder) {
EditingContext.initObjectWithCoder(this, aDecoder);
}
The enterprise object simply passes on responsibility for archiving and
unarchiving itself to the EOEditingContext class, by invoking the
encodeObject:withCoder:
and
initObject:withCoder:
class methods and passing a reference to
itself (
self
) as one of the arguments. The editing context takes care of the rest.
(See the EOEditingContext class specification in the Enterprise Objects Class
Reference for more information.)
Archiving Custom Objects in Other Applications
Custom classes that can’t take advantage of an EOEditingContext for archiving
must take a different approach. These classes still must conform to the
NSCoding protocol and implement its
encodeWithCoder:
and
initWithCoder:
methods;
however, you must implement them differently. In
encodeWithCoder:
, you use the
coder argument provided to encode the object’s instance variables. In
initWithCoder:
, the object uses the decoder provided to initialize itself.