Creating Workflows

What is a Workflow?

A Workflow is a description for a sequence of FlowRepresentables. Those operations normally display screens or views. There are different ways of creating workflows in SwiftUI and creating workflows in UIKit.

What Is a FlowRepresentable?

A FlowRepresentable is a protocol that describes a type that can appear in a Workflow. It optionally declares FlowRepresentable.WorkflowInput and FlowRepresentable.WorkflowOutput, indicating if it requires data or passes data forward. It also provides an initializer so that it can be created by the Workflow. Once an item conforms to FlowRepresentable, it can be injected into any Workflow.

Valid Workflow Sequences

Workflows enforce (either at compile-time or run-time) that the sequence of FlowRepresentables is well-formed. For example, if a particular FlowRepresentable has a FlowRepresentable.WorkflowInput of String, but the previous item passes an Int forward, that workflow is malformed. A FlowRepresentable can declare a FlowRepresentable.WorkflowInput of Never if it does not take in data, Any if it can intake multiple types, or AnyWorkflow.PassedArgs if it can handle being passed no data, or data of any kind. Lastly, if your FlowRepresentable.WorkflowInput needs to take in multiple values, you can use a tuple.

In some cases, like UIKit, the compiler is efficient enough to give you compile-time feedback if a workflow is malformed. This means that run-time errors are rare. They can still occur; for example, if you have Item1 declare a FlowRepresentable.WorkflowOutput of AnyWorkflow.PassedArgs, then call FlowRepresentable.proceedInWorkflow(_:) with .args("string"), but Item2 has a FlowRepresentable.WorkflowInput of Int, there’ll be a run-time error because the data passed forward does not meet expectations.

In SwiftUI, the compiler was not efficient enough to give the same compile-time feedback on malformed workflows. When that safety was added, the compiler only allowed for small workflows to be created. To combat this, SwiftUI is heavily run-time influenced. When you create a WorkflowView, the library performs a run-time check to guarantee the workflow is well-formed. This means that if you wanted to test your workflow was well-formed, all you need to do is instantiate a WorkflowView.