Running TCA headless on a background thread

I understand it makes sense to run TCA on the main thread for SwiftUI and UIKit but it is not always desired. For example in one of my apps, we use Texture/AsyncDisplayKit; the configuration and layout of cell nodes is done entirely in the background. We also have a synchronisation task that processes a lot of data from remote. I'd love to refactor the synchronisation code to use TCA, however our user experience will definitely suffer dramatically if that processing would happen on the main thread.

@stephencelis @mbrandonw Is there a way run TCA on a thread other than the main thread?

We try to note this in the README's FAQ, but TCA does not have an opinion on threading, and does no work to run on the main thread. The fact that it typically runs on the main thread in SwiftUI applications is due to the fact that most SwiftUI actions are sent on the main thread. If you want to run TCA on a background queue, you need to send actions to the store on that queue, and your reducer must receive effects on that queue.

Generally, reducers should be lightweight enough to run on the main queue. If you have a task that processes a lot of data that might block your reducer, you can move that work into an Effect and feed the result back through via an action.

1 Like

Thanks! That's great!

Terms of Service

Privacy Policy

Cookie Policy