I'm writing a simple executableTarget in swift package to test RunLoop and DispatchQueue.
// main.swift
import Foundation
let rl = RunLoop.current
let timer = Timer(fire:Date(), interval: 1, repeats: true) { timer in
print("loop start")
DispatchQueue.main.async {
print("never runs")
}
print("loop end")
}
rl.add(timer, forMode: RunLoop.Mode.common)
rl.run()
The RunLoop works fine, but the DispatchQueue.main.async block Never runs.
Then I create another project of a macOS Command Line Tool with the same code above.
The DispatchQueue.main.async works.
I'm wondering why is that, and how to make the swift package's executableTarget works.
The swift package config is like this:
// Package.swift
// swift-tools-version:5.5
// The swift-tools-version declares the minimum version of Swift required to build this package.
import PackageDescription
let package = Package(
name: "Concurrency",
platforms: [
.macOS(.v12)
],
dependencies: [],
targets: [
.executableTarget(
name: "Concurrency",
dependencies: []),
.testTarget(
name: "ConcurrencyTests",
dependencies: ["Concurrency"]),
]
)
Thanks.