Archiving Endpoints

Endpoints all conform to the NSCoding protocol allowing them to be archived and unarchived using the standard NSKeyedArchiver and NSKeyedUnarchiver.

Archiving an endpoint provides an easy way to save a user's choice of source or destination endpoints to a document file or a to an application's preferences.

The following example shows how to archive an endpoint:

NSMutableData*   data;
NSKeyedArchiver* archiver;
    
data = [NSMutableData data];
archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];

[archiver encodeObject:myEndpoint forKey:@"myEndpoint"];

[archiver finishEncoding];
[archiver release];

The following example shows how to unarchive an endpoint:

NSKeyedUnarchiver* unarchiver;
PYMIDIEndpoint*    myEndpoint;

unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];

myEndpoint = [[unarchiver decodeObjectForKey:@"myEndpoint"];

[unarchiver finishDecoding];
[unarchiver release];

Archiving Real Endpoints

Archiving and unarchiving real endpoints is generally transparent. Real endpoints are not actually created on unarchiving. Instead, an existing real endpoint that matches the archived endpoint is found and returned by the unarchiver.

If the device corresponding to an endpoint is not available when the endpoint is unarchived, an endpoint is created but is marked as offline. The endpoint will be reconnected with its device if the device is later plugged in.

Archiving Virtual Endpoints

Unlike real endpoints, new virtual endpoints are created when they are unarchived.

Often, for the purposes of undo or pasteboard handling, you will need to archive references to endpoints and have those existing endpoints found on unarchiving rather than having new endpoints created. To facilitate this, an object of the PYMIDIEndpointSet class can be used as a delegate for an NSKeyedArchiver or NSKeyedUnarchiver. Any archived endpoints that appear in the set will be archived by reference. The same set can then be used when unarchiving to find the matching existing endpoints.