Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save grigorye/dbbfaddfed71a8bd95621258c04fd3ba to your computer and use it in GitHub Desktop.
Save grigorye/dbbfaddfed71a8bd95621258c04fd3ba to your computer and use it in GitHub Desktop.
RunLoop.main.perform vs DispatchQueue.main.async
class RunLoopPerformVsDispatchQueueAsyncTests: XCTestCase {
func testRunLoopPerformTakesPriorityOverDispatchAsync() {
enum Event {
case dispatchQueueAsync
case runLoopPerform
}
var events: [Event] = []
DispatchQueue.main.async {
events.append(.dispatchQueueAsync)
}
XCTAssertEqual(events.count, 0)
let mainRunLoop = RunLoop.main
mainRunLoop.perform {
events.append(.runLoopPerform)
}
XCTAssertEqual(events.count, 0)
repeat {
mainRunLoop.run(mode: .default, before: .distantPast)
} while events.count < 2
XCTAssertEqual(events, [.runLoopPerform, .dispatchQueueAsync])
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment