沒有區別後立即執行。我通過調用100次來測試,並且回調按照它們入隊的順序執行。
我進一步測試了100個隊列,看看它如何表現並行性,並且回調仍然按照入隊順序執行。因此,結論是asyncAfter(.now())與async相同:
func testOneThread() {
var lastCallbackRun = 0
for i in 1...100 {
DispatchQueue.main.asyncAfter(.now()) {
assert(lastCallbackRun < i)
lastCallbackRun = i
if i == 100 {
NSLog("done")
}
}
}
}
func testAsync() {
for _ in 0...100 {
let q = DispatchQueue(label: "whatever")
q.async(execute: testOneThread)
Unmanaged<DispatchQueue>.passRetained(q)
}
}
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
testAsync()
// The rest of it.
}
}
非常感謝。 –