# Running TCA headless on a background thread

**URL:** <https://forums.swift.org/t/running-tca-headless-on-a-background-thread/40112>\
**Category:** Swift Composable Architecture\
**Created:** [September 8, 2020, 2:16pm UTC](https://forums.swift.org/t/running-tca-headless-on-a-background-thread/40112 "2020-09-08T14:16:41Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![mycroftcanner](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/mycroftcanner/32/12683_2.png) [@mycroftcanner](https://forums.swift.org/u/mycroftcanner)\
**Post date:** [September 8, 2020, 2:16pm UTC](https://forums.swift.org/t/running-tca-headless-on-a-background-thread/40112/1 "2020-09-08T14:16:41Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![stephencelis](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/stephencelis/32/2675_2.png) [@stephencelis](https://forums.swift.org/u/stephencelis)\
**Post date:** [September 8, 2020, 2:38pm UTC](https://forums.swift.org/t/running-tca-headless-on-a-background-thread/40112/2 "2020-09-08T14:38:36Z")

</div>

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.

> [@mycroftcanner](#):
>
> 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.

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.

---

<div class="post-metadata">

**Author:** ![mycroftcanner](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/mycroftcanner/32/12683_2.png) [@mycroftcanner](https://forums.swift.org/u/mycroftcanner)\
**Post date:** [September 8, 2020, 2:48pm UTC](https://forums.swift.org/t/running-tca-headless-on-a-background-thread/40112/3 "2020-09-08T14:48:53Z")

</div>

Thanks! That's great!

---

<div class="post-metadata">

**Author:** ![barcis89](https://avatars.discourse-cdn.com/v4/letter/b/9f8e36/32.png) [@barcis89](https://forums.swift.org/u/barcis89)\
**Post date:** [November 9, 2020, 12:27pm UTC](https://forums.swift.org/t/running-tca-headless-on-a-background-thread/40112/4 "2020-11-09T12:27:27Z")

</div>

> [@stephencelis](#):
>
> Effect

@stephencelis could you share some example how Effect can be run on background thread?

In my case I have some quite large collections which I have to iterate. So would be cool to move this opertaions to backround thread.

Thank you in advance for any suggestion!

---

<div class="post-metadata">

**Author:** ![eimantas](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/eimantas/32/13145_2.png) [@eimantas](https://forums.swift.org/u/eimantas)\
**Post date:** [November 16, 2020, 4:03am UTC](https://forums.swift.org/t/running-tca-headless-on-a-background-thread/40112/5 "2020-11-16T04:03:26Z")

</div>

> [@barcis89](#):
>
> could you share some example how Effect can be run on background thread?

This would be a great case-study/question on its own, however I imagine this work being a part of an environment:

```swift
struct FooEnvironment {
    var scheduler: AnyScheduler<DispatchQueue>
    var doJob: (String, AnyScheduler<DispatchQueue>) -> Effect<Result,Error>
}

```

When creating this environment you could pass any scheduler you want (you could use main, test or custom dispatch queue here) then in reducer you could do something like this:

```swift
case .doHeavyLifting(let param):
    return fooEnv.doJob(param, fooEnv.scheduler)
        .map(.didAGreatJob) // FooAction.didAGreatJob(Result)
        .catch { Just(.failedToDoJob($0.localizedDescription) } // in case of error
        .eraseToEffect()

```

---

<div class="post-metadata">

**Author:** ![mycroftcanner](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/mycroftcanner/32/12683_2.png) [@mycroftcanner](https://forums.swift.org/u/mycroftcanner)\
**Post date:** [November 30, 2020, 6:42pm UTC](https://forums.swift.org/t/running-tca-headless-on-a-background-thread/40112/6 "2020-11-30T18:42:53Z")

</div>

@stephencelis If I use a recursive reducer and navigate a few level down. Processing actions will slows down my scrollview drastically when an action is processed. Disabling .debug logging helps a little but not much.

I extended view store send to use a background thread and the user interaction lag disappeared completely.

objectWillChange.send() is now called from a background thread and that is not allowed. I think TCA should be modified so that objectWillChange.send() is always called on the main thread.

[https://github.com/pointfreeco/swift-composable-architecture/pull/327](https://github.com/pointfreeco/swift-composable-architecture/pull/327)

---

<div class="post-metadata">

**Author:** ![barcis89](https://avatars.discourse-cdn.com/v4/letter/b/9f8e36/32.png) [@barcis89](https://forums.swift.org/u/barcis89)\
**Post date:** [December 2, 2020, 11:57am UTC](https://forums.swift.org/t/running-tca-headless-on-a-background-thread/40112/8 "2020-12-02T11:57:48Z")

</div>

Thank you @eimantas for your reply!!!!
