Asynchronous runloop?

In the paper written about unstructured tasks:

We might need to ... initiate asynchronous work from synchronous code...

So if I understand right, If we want to initiate a concurrency job from the main thread method of UIKit View controller View did load is only to detaching Task, because the run loop of UIApplication is synchronous. Also there written

they remain a very important building block especially for more free-form usages and integration with legacy APIs.

Also If I see right, there is nothing to start a UIKit or AppKit application asynchronously, something like this

struct App {
    static func main() async {
        async let k = App().runLoop(number: 1)
        async let n = App().runLoop(number: 2)
        print("DONE: \(await k), \(await n)")
    func runLoop(number: Int) async -> Int {
        var line = ""
        while line != "done" {
            if let l = readLine() { // wait event
                line = l
                // render
                print("command: \(line) in runloop: \(number)")
        return 0


Therefore, I would to ask if it is worth expecting that someday there will be async method of NSApplication's run():

class NSApplication {
    func run() async {}

Or am I wrong in understanding async?

so in your first example there should be a suspension point in that loop (preferably at that read point since that should be async). Doing so will allow the two loops to run free and not interfere with execution on the main actor. That main actor is extensibly isomorphic to the main thread, which has a main NSRunLoop.

Your instincts are correct with regards to NSApplication, I could imagine a world that would be possible (without trying myself - I think that can actually be done even outside of changes to AppKit, so you might actually be able to write that in your own application today).

Terms of Service

Privacy Policy

Cookie Policy