Preserving Patch State

cwright's picture

Sometimes, patches dynamically create input and output ports. Sometimes, they have instance-specific configuration.

To preserve this kind of data across restarts of QC, we resort to saving our patches "state." This is how all the patches store their configuration so that they start up again in the same condition.

By default, any patch that inherits from QCPatch gets basic port-value state saving. This saves the values provided to ports when you double-click them and enter text.

But for complex state saving, like dynamic ports, Inspector panel values, and the like, we resort to using QCPatch's (Override) category methods, -(NSDictionary)state and -(BOOL)setState:(NSDictionary)state.

When implementing state, it is essential that you call [super state], since that adds a few key-value pairs to your dictionary (timebase information and user information, as well as port-value data, specifically). You are then free to add other key-value pairs to that dictionary, and then return it. That's pretty simple.

When QC starts up (or your patch gets loaded in some other QC-aware environment), setState is called with the returned NSDictionary. This method should set data appropriately, and return YES if it completes without any problems. Returning NO causes QC to pop up a warning dialog stating that one of the patches failed to restore.

That's about all there is to it!