Workflow Example: iCloud Persistent Variable

Sometimes you want to maintain data for longer than the duration of a single workflow run. Perhaps you want to access data on subsequent runs of the same workflow or from a run of a different workflow. This is where we want to take advantage of persistent variables. This example shows how persistent variables can effectively be provisioned by saving data to a file in iCloud and reading it back in. It would be great to do this as JSON as this would easily allow multiple variables to be saved in a single file, but until workflow allows you to write back individual keys into a JSON dictionary it’s just much easier to store one data item per file.

The workflow begins by attempting to read in a file that is to hold the variable data. If the file exists it will be stored in a variable. If it doesn’t the variable is initialised to zero.

The variable is incremented and saved back to the file. The file is created if it does not already exist and overwritten if it does. The workflow output is the incremented value.

Tags: | workflow |