Redux-like architecture with SwiftUI: Side Effects

In the previous post we went through setting up the basic architecture for iOS with Redux-like approach, and now we will be following up with additional optimizations and support for asynchronous functions or side effects.

So far, our app is aligned with Redux three principles:

  • A single source of truth
  • A read-only App State
  • Mutating the state only through Reducer functions.

What happens if we want to load animal names from the network instead of a local array?

Sure, we could add all that logic and dependencies to our appReducer, and make it work for this small app, but it’s far from optimal and goes against Redux principles.

By definition, Reducers must be pure functions—functions that return the exact same output for given inputs, and should also be free of side effects like API calls or asynchronous tasks, so let’s introduce the Middleware concept.

This project is based on the code we wrote in the previous post. Before starting, grab a clean copy from this repository.


In Redux, a Middleware is an entity (function) that is executed when certain Actions go through the Store. It receives a copy of the current AppState, performs some operations (API calls, or async tasks), and dispatches a new Action.

Middlewares take care of all the heavy load in the background (like API call, database fetches and updates, etc…) while Reducers simple deal with updating our state synchronously. Let’s update the store to support Middlewares.


I’ve created a new typealias to define our Middleware, and then added a Middleware array to our Store. This allows us to inject as many Middleware functions as we need on initialization.

Then, I’ve modified our dispatch function to iterate on the available Middlewares. Each Middleware will receive a copy of our State and an Action, and will return a Combine Publisher containing a new Action, which will be dispatched upon reception.

So now, instead of generating animal names in our reducer, let’s go asynchronous and create a Service that simulates a network call, by generating an animal name after X(random) number of seconds.


Here we are using the Combine “Future Publisher” to create a promise, that is fulfilled after a random number of seconds with a random name. As you can see, we are returning a publisher of type AnyPublisher and not a String. On completion, the Future publisher produces a single String value, and then finishes or fails. In this case, it never fails, so we set it up as <String, Never>

By using AnyPublisher, instead of Publisher we wrap our publisher, so its details are not exposed, and also prevent its callers from accessing send(:) on it. If you want to find out more about this, check out this Stack Overflow thread.

Now that the Store is ready for async tasks, and we have our new service, let’s move forward with optimizing our Redux Stack and implementing our first Middleware.

Let’s do some Optimizations

We could store everything in our AppState struct, and that works for a small app like this one, but as apps grow you end up passing a huge amount of data around and things get out of hand.


Here we are simply dividing our state into different pieces and composing them in AppState. You can continue to add as many structs as needed to keep things clean and easy to understand.

Now let’s setup the Actions for the animal generator. Since now we are fetching the data asynchronously, we’ll need two: One that performs the fetch, and another that sets the result value in our State when the data arrives.

As we did with our State, let’s do some composition here too.


Following the same pattern, we can also extract the logic for our reducers.


With those changes in place we will keep things tidy and clean, when the app grows, and limit the data we pass around. The app won’t compile as we have not updated our views, but let’s move onto the Middleware and we can update that later.

This is also a good time to reorganize our functions and Types into different files and groups. Here’s an example that would work for most small to medium size apps.

Implementing our Middleware

The animalMiddleware will receive a copy of the current State and an Action and returns a Publisher. We will be using the new service we’ve created, to fetch an animal, and generate a new Action, that will be sent back for dispatch.


Pretty neat, huh?. Since we are using Combine, there is no need for callbacks or weird tricks to handle asynchronous operations and as our application grows, we can add more cases to our Middleware to intercept other actions.

It also helps map over the resulting data, and dynamically generate an Action to be dispatched.

Now let’s modify our Store initializer to inject our Middleware as a dependency.


In essence, we are injecting our new AnimalState and the Middleware in the initializer. This approach makes our code really easy to test

We’ve also changed the dispatch call in our initializer to match the changes we did to our Store.

Now, let’s take care of updating the view.


That’s it. If you try build and run the app, it will launch, and after some seconds, a new Animal will appear on the screen, and the same will happen when you tap the button.

It’s all good, but the User Experience not so much. It takes time to display an animal after you press the button, and you have no feedback on the screen. So let’s update our UI to display a “Loading” message, while our animal is fetched.

The easiest way to do this, would be displaying a “Loading” message in the same label, while the animal name is being loaded, so let’s modify our animalReducer.

As you can see, I’m just modifying our state when that actions arrives. Now try and run the app again, and you should get something like this:

The Power of Middlewares

Having Middlewares in place, allows us to do all sorts of things in the application, as we can easily intercept any action that passes through our Store.

For example, creating a Middleware, that Logs every action to the console is as simple as writing a function.

And then simply add it to your store Initialization

Remember that every Middleware receives a full copy of you app’s state, and can trigger whatever action you need, so possibilities are limitless.

Note on Side Effects, Middlewares and Thread Safety

This approach allows us to add as many Middlewares we need, but you need to consider that everything we are doing is asynchronous. All middlewares run at the same time, with an identical copy of the current state.

Given that we will have multiple threads running in parallel, you should pay special attention to what you do at middleware level. In general, you should avoidintercepting the same Action in different Middlewares, as there is no thread synchronization between them, and you will certainly face some race conditions.

A way to fix this issue will be running our Middlewares in sequence, with each one waiting for the previous one to complete, but that also impacts performance.

Perhaps there’s good material there for another post.


We did a lot of things in this second part of the tutorial and now you have a really robust Architecture, that would allow you to write testable apps in no time.

Check out the resulting app for this post is the repo., where we will be implementing proper error handling, so our users can be informed if something has gone wrong.

I hope you enjoyed this tutorial. If you have any questions or comments, feel free to ping me on Twitter.

Posts in this series

  1. The Basics
  2. Side Effects (This Post)
  3. Error Handling
  4. Real World App

Sources & Refs:

Leave a Reply

Your email address will not be published. Required fields are marked *