简体   繁体   English

RunLoop如何适配swift并发(async/await)

[英]How to adapt RunLoop to swift concurrency(async/await)

I'm having some compilation warnings using Xcode 14.2/swift 5.7 (future errors in swift 6).我有一些使用 Xcode 14.2/swift 5.7 的编译警告(swift 6 中的未来错误)。

I've some async function tests in a unit test target which include some code to process UI changes in the main loop.我在单元测试目标中有一些异步 function 测试,其中包括一些代码来处理主循环中的 UI 更改。

There are two related warnings:有两个相关的警告:

RunLoop.current.run(until: Date())
// which raises next warning
Class property 'current' is unavailable from asynchronous contexts; currentRunLoop cannot be used from async contexts.; this is an error in Swift 6
CFRunLoopRunInMode(CFRunLoopMode.defaultMode, 0.1, false)
// which raises next warning
Global function 'CFRunLoopRunInMode' is unavailable from asynchronous contexts; CFRunLoopRunInMode cannot be used from async contexts.; this is an error in Swift 6

Here a full function test example.这里有一个完整的 function 测试示例。

@MainActor
public class MainViewController: UIViewController {
...
    public func showLoading() {
    }
...
}
...

@MainActor
func testBasic() async throws {
    // GIVEN
     sut = MainViewController(nibName: nil, bundle: nil)
     present(viewController: sut)

    // WHEN
     sut.loadViewIfNeeded()
     sut.showLoading()
     RunLoop.current.run(until: Date())

     sut.hideLoading()
     sut.showNoConnection()
     RunLoop.current.run(until: Date())

    // THEN
     XCTAssertTrue(sut.connectionStatus.isHidden == false)
}

What alternatives would you use at this point to fix the warnings?此时您会使用什么替代方法来修复警告?

So far, my understanding is that Runloop methods have been declared not async safe (which is understandable), but without adding an async alternative so far.到目前为止,我的理解是 Runloop 方法已被声明为非异步安全(这是可以理解的),但到目前为止还没有添加异步替代方法。

So, at the moment, there are no fixes to this warnings, and instead it would be needed to avoid using Runloops in async contexts.因此,目前,没有针对此警告的修复,而是需要避免在异步上下文中使用 Runloops。

PD: This question was crossposted in an Apple forum post named How to adapt RunLoop to swift concurrency(async/await) PD:这个问题交叉发布在名为How to adapt RunLoop to swift concurrency(async/await)的 Apple 论坛帖子中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM