PATH Documentation > WebObjects

Up Previous Next

Detecting Changed Attributes

Synopsis

Describes how to find out what attributes have changed in an object before those changes are committed.

Description

By default, before an enterprise object modifies its state, it sends itself a willChange message which, in turn, causes the objectWillChange notification to be sent to all registered observers, particularly the object's editing context. In response, the editing context records a snapshot of the object's state before the modification takes place, allowing later comparisons against the modified object to determine precisely which modifications were made. However, the objectWillChange notification is generic and does not specify the attribute of the object that is to be changed.

With a simple technique, you can determine which attributes of an object have changed before these changes are committed to the database. First, as a delegate of EOEditingContext, implement a suitable delegate method such as editingContextWillSaveChanges . In this method, assuming that your Enterprise Object descends either from NSObject (in Objective-C) or from the EOCustomObject class in Java, you can first determine the original snapshot using the following EOEditingContext method:

Objective-C Code

 
- (NSDictionary *)committedSnapshotForObject:(id)object;

Java Code

 
public native com.apple.yellow.foundation.NSDictionary committedSnapshotForObject (com.apple.yellow.eocontrol.EOEnterpriseObject);

You can next get all the changes to the object's original snapshot.

Objective-C

 
- (NSDictionary *)changesFromSnapshot:(NSDictionary *)snapshot;

Java Code

 
public native com.apple.yellow.foundation.NSDictionary changesFromSnapshot (com.apple.yellow.foundation.NSDictionary);

The NSDictionary object returned from the changesFromSnapshot method (in both languages) only contains the attribute names and their values that refer to uncommitted changes in the object. If there is a to-many attribute, the uncommitted value is an array of two arrays: uncommitted additions and uncommitted deletions.

The following Java sample code gets the NSDictionaries reflecting the prior committed state of an object and its currently changed attributes, based on the API described above:

Java Code

 
/* Assuming that myCustomObject descends from EOCustomObject and already exists */ 
EOEditingContext ec = myCustomObject.editingContext(); 
// The committedSnapshotForObject method expects to have 
// an EOEnterpriseObject as argument, so typecasting is necessary here. 
NSDictionary  originalSnapshot = ec.committedSnapshotForObject((EOEnterpriseObject)myCustomObject); 
NSDictionary changesDict = myCustomObject.changesFromSnapshot(originalSnapshot);

To view all changes recorded in the changed snapshot, you could use the following sample code:

Java Code

 
int count; 
count = changesDict.count(); 
NSArray allKeys = changesDict.allKeys(); 
java.util.Enumerator anEnum = changesDict.keyEnumerator(); 
while (anEnum.hasMoreElements()) { 
    String key = anEnum.nextElement(); 
    String value = changesDict.valueForKey(key); 
    System.out.println("Changes: Key " 
	 + key + "Value " 
	+ value); 
}

See Also

Questions

Keywords

Revision History

14 July, 1998. Mai Nguyen. First Draft.

17 November, 1998. Terry Donoghue. Second Draft.


© 1999 Apple Computer, Inc.

Up Previous Next