Sei sulla pagina 1di 6

Using Repeat loops

Workflow offers two actions for creating repeat loops, Repeat and Repeat
with Each, both in the Control Flow category. These actions allow you to run
a group of actions multiple times in a row.

The Repeat action runs the actions placed inside of it a specified number of
times.

When you drag a Repeat action into your workflow, you’ll notice it comes in
two parts: Repeat and End Repeat. Place the actions you want to repeat
between these markers.

For example, you could have a repeat loop that contains a Vibrate Device
action, and set it to repeat three times. When run, the device would vibrate
three times.
You can place as many actions inside the repeat loop as you’d like, and they’ll
run in order from top to bottom. Once the last action in the loop is reached,
the workflow will jump back to the first action in the loop for the next
iteration, as many times as necessary.

Input flow
When you pass input into the Repeat action, that same input is passed into
the beginning of the loop for each iteration. For example, if you have a
workflow with a Text action followed by a repeat loop that contains a Speak
Text action, the workflow will speak the same text multiple times.

For each iteration of a repeat loop, the last action’s output is gathered into a
list, and that list becomes the output of the Repeat action as a whole.

For example, say you have a Repeat action that runs four times and contains
a Get Latest Photos action. In each iteration of the loop, the Get Latest
Photos action will output the most recent photo saved to the Camera Roll.
After all four iterations, the outputs are gathered together, creating a list of
four of the same photo. Those four photos become the output of the Repeat
action as a whole. They are then passed to the next action after the repeat
loop.

To learn more about the flow of inputs and outputs, see How workflows work.

Pro Tip

Tap the empty space around the plus and minus buttons to set the number
of repetititons to a variable. When set to Ask When Run, you’ll be
prompted to choose how many times the loop should repeat when the
workflow runs.

The Repeat with Each action takes a list of items as input, and runs the
actions inside once for each item in the list. It’s useful if you are working with
multiple files or pieces of content and want to run the same group of actions
for each of them. This type of loop repeats until every item that was passed
into the Repeat with Each action has been iterated over.

For example, if you have a Get Upcoming Events action set to retrieve the
next four upcoming calendar events, and you place a Repeat with Each
action after, the repeat loop will run four times and each iteration will receive
one of the photos as input.
At the end of the repeat loop, the output passed to the End Repeat action
during each iteration is gathered together and passed as the output of the
Repeat with Each as a whole.

The Repeat Item variable

Repeat with Each loops offer a special variable for tracking the current
item: Repeat Item. The Repeat Item variable updates with each iteration of
the loop, containing the item passed as input for that iteration.

If you were to pass ten calendar items into a Repeat with Each action, the
Repeat Item variable would represent one of the calendar events in each
iteration as the workflow loops through them one-at-a-time.

Repeat Item is only available to choose from the variable bar and tray, plus
any variable pickers; you will not see it in the Magic Variable picker.

Note

If you’ve placed a Repeat with Each action inside another Repeat with
Each action, the variable name will change to Repeat Item 1 to represent
the inner repeat item, Repeat Item 2 for the deeper loop, and so on.
All repeat loops offer a special variable to keep track of the current iteration:
Repeat Index. The Repeat Index variable contains the number of times a loop
has been repeated so far, starting at 1 for the first iteration of the loop and
incrementing by one each time. The second time a loop runs, the Repeat
Index will be 2, the third time it will be 3, and so on.

Note

If there are nested repeat loops (one Repeat action inside another), the
variable name will change to Repeat Index 1, Repeat Index 2, and so on.

To learn more about variables, see Using variables.

Workflow does not offer an action to repeatedly run a group of actions


forever. However, you can emulate this behavior by using the Run
Workflow action, which lets you run one workflow inside of another.
Configure the Run Workflow action to run the same workflow that it’s
contained in, and the workflow will run from start to finish repeatedly until
you stop it.

When you’re getting started with Repeat and Repeat with Each, it’s
helpful to test the results produced after End Repeat and see if the content
matches what you intended. If you place a Quick Look action after End
Repeat, you can preview the items that the repeat loop outputted.

You could also place a Show Alert action after End Repeat and in the
message box insert a “Repeat Results” Magic Variable to display text that was
outputted by the loop. If you’re testing a loop that’s in the middle of a long
workflow, it’s helpful to use Show Alert to preview results, as you can
include a “Cancel” button in the alert to end the workflow at that point.

Move on to the next section to learn about Using Find and Filter actions.

Potrebbero piacerti anche