# Async/Await: is it possible to start a Task on @MainActor synchronously?

**URL:** <https://forums.swift.org/t/async-await-is-it-possible-to-start-a-task-on-mainactor-synchronously/52862>\
**Category:** Using Swift\
**Tags:** asyncawait\
**Created:** [October 16, 2021, 9:43am UTC](https://forums.swift.org/t/async-await-is-it-possible-to-start-a-task-on-mainactor-synchronously/52862 "2021-10-16T09:43:41Z")\
**Posts on this page:** 1\
**Showing post:** 9

<div class="post-metadata">

**Author:** ![gwendal.roue](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/gwendal.roue/32/13992_2.png) [@gwendal.roue](https://forums.swift.org/u/gwendal.roue)\
**Post date:** [October 18, 2021, 1:06pm UTC](https://forums.swift.org/t/async-await-is-it-possible-to-start-a-task-on-mainactor-synchronously/52862/9 "2021-10-18T13:06:03Z")

</div>

Maybe `MainActor` will change its behavior, and start running synchronously when it can, yes.

But if I understand @ktoso well, at the time custom executors become public, we'd become able to write, _outside of the stdlib_, in our own application or library code, a custom global actor that does what we need:

```swift
@MainActor // (sic)
func someSynchronousUIKitOrSwiftUICallback() {
  Task { @UIActor in // (sic) a custom global actor
    for await value in someAsyncSequence {
      // handle value
    }
  }
  // <- Here values that were produced synchronously
  // are already handled, and UIKit/SwiftUI has been
  // correctly instructed.
}

```

This custom `UIActor` would play exactly the same role as those existing tools:

- [UIScheduler](https://github.com/pointfreeco/combine-schedulers/blob/0.5.3/Sources/CombineSchedulers/UIScheduler.swift)
- [ConcurrentMainScheduler](https://github.com/ReactiveX/RxSwift/blob/6.2.0/RxSwift/Schedulers/ConcurrentMainScheduler.swift)
- [MainScheduler](https://github.com/ReactiveX/RxSwift/blob/6.2.0/RxSwift/Schedulers/MainScheduler.swift)

They exist for a well identified reason: they help developers program asynchronously _within the constraints of frameworks like UIKit or SwiftUI_.

In UIKit or SwiftUI, a missed opportunity from one of their synchronous callbacks usually means a degraded experience for the app user or the developer, from a blank frame on screen, to a need to explicitly code against double-taps.

Combine and RxSwift exist independently of UIKit or SwiftUI, and yet they provide the sharp and necessary tools for a seamless integration. I'm very eager to see Swift concurrency fill the gap with custom executors.

---

_[View the full topic](https://forums.swift.org/t/async-await-is-it-possible-to-start-a-task-on-mainactor-synchronously/52862)._
