
[英]Why does Swift not resume an asynchronous function on the same thread it was started?
在“The Swift Programming Language”并发章节的介绍部分,我读到: 当异步 function 恢复时,Swift 不保证 function 将在哪个线程上运行。 这让我很吃惊。 这看起来很奇怪,例如与 pthreads 中等待信号量相比,执行可以跳转线程。 这使我想到以 ...
[英]Why does Swift not resume an asynchronous function on the same thread it was started?
在“The Swift Programming Language”并发章节的介绍部分,我读到: 当异步 function 恢复时,Swift 不保证 function 将在哪个线程上运行。 这让我很吃惊。 这看起来很奇怪,例如与 pthreads 中等待信号量相比,执行可以跳转线程。 这使我想到以 ...
[英]XCTest async code doesn't get executed in 'tearDown() async throws' method
从 Apple 的关于setUp和tearDown的 XCTest 文档中,它说在tearDown() async throws方法中执行异步代码。 在我的例子中,我使用 API 调用在服务器中创建了一些测试数据,我想在测试用例执行完成后清理它。 根据我的理解,理想的地方是tearDown 。 然后 ...
[英]Data race occurring in Swift Actor
我通过使用 Thread Sanitizer 在我的 Swift 应用程序中发现了数据竞争,因此我第一次尝试通过将有问题的class转换为actor来修复竞争条件。 竞争导致的崩溃似乎已经消失,但 Thread Sanitizer 仍然说代码中存在数据竞争,我认为这在actor中应该是不可能的。 ...
[英]How I can solve issue with assigning view function to StateObject propety
我正在尝试像这样分配常规 function 即在swiftui视图中定义到@StateObject var SpeechSynthesizer 有财产的 但我收到警告将“@MainActor (AVSpeechUtterance) -> ()”类型的 function 值转换为“(AVSpee ...
[英]Can we define an async/await function that returns one value instantly, as well as another value asynchronously (as you'd normally expect)?
想象一个图像加载 function 并完成关闭。 假设它返回一个令牌 ID,您可以在需要时使用它来取消异步操作。 我们如何使用 swift 并发(优雅地)完成此操作? 它甚至可能或实用吗? ...
[英]Why does a @MainActor annotated class initialisation gives the error
我有一个名为 Person 的类,它带有 @MainActor 注释。 但是当我尝试创建这个 mainactor 类的实例时,它给出了一个错误。 “在同步非隔离上下文中调用主要参与者隔离初始化器‘init(firstName:lastName:)’”。 但是,如果我创建 UILabel() 实例, ...
[英]Difference of TaskPriority for Task.cancel()
我试过这篇文章https://www.swiftbysundell.com/articles/building-an-async-swiftui-button/代码来自哪里,但在结构中添加了优先级常量。 如果我使用优先级,则按钮正在做它应该做的事情(只在睡眠时间结束时显示 ProgressView, ...
[英]Swift concurrency: Why is this method async?
我正在努力思考如何将 Swift 并发与使用基于块的东西(如 Timer)的旧代码集成。 因此,当我构建下面的代码时,编译器在self.handleTimer()行告诉我Expression is 'async' but is not marked with 'await' 为什么它是异步的? 它 ...
[英]Add multiple events to the Calendar with Concurrency
我创建了一个 function 以使用 EventKit 将我的课程事件添加到日历应用程序。 在学习了 swift 并发后,我想更新我的代码来使进度更快,即使用分离任务或 TaskGroup 来添加这些事件。 在没有分离任务或任务组的情况下同步代码:func export_test() { ...
[英]is hashValue of Task unique on Swift concurrency?
我想要一个 SomeProtocol 数组,它有一个 get 属性表示在任务扩展中。 如果 Task 的 hashValue 是唯一的,我想将其视为删除 SomeProtocol 数组中对象的主键。 而且我想知道它是否也在内存级别上得到保证。 这是下面的代码。 ...
[英]Swift Concurrency: Notification Callbacks on @MainActor Objects
语境在使用Swift 5.x和Xcode 14构建的Mac应用程序中,我有一个@Published @MainActor 问题当 Mac 进入睡眠状态时,此应用程序需要采取某些操作,因此我在init()方法中订阅了适当的通知,但由于AppController装饰有@MainActor ,我收到此警告 ...
[英]Can a class be Sendable if it holds a reference to a non-Sendable singleton?
假设我有一个 class,它只有 state 是对 singleton class 的引用:class MyClass { let networkHelper: NetworkHelper func makeNetworkRequest(_ url: URL) { ...
[英]How to convert an atypical completion handler to Swift Concurrency
我的代码中有一些用完成处理程序编写的异步方法,我想将其转换为新的 Swift 并发语法。 通常这只是简单地将调用包装在withCheckedThrowingContinuation中并分别调用resume(throwing:)或resume(returning:)而不是completion(nil, ...
[英]Awaiting Task Completion in SwiftUI View
我正在使用显示位置列表的视图。 当用户点击一个位置时,一个包含Task的didSet块在一个单独的 class 中被触发,该块用@ObservedObject属性包装: 设置networking.lastLocation后触发的Task如下: 与冻结我的应用程序的 UI 一秒钟相比,有什么更好的方法 ...
[英]How come a MainActor isolated mutable stored property gives a sendable error?
我正在尝试使 class 符合Sendable 。 我有一些导致问题的可变存储属性。 但是,我无法理解的是 MainActor 隔离属性不允许我的 class 符合 Sendable。 但是,如果我将整个 class 标记为@MainActor ,那就没问题了。 但是,我实际上并不想让整个 cla ...
[英]Synchronous function inside Task or outside - any difference?
运行未标记为异步的 function 和在Task中运行它会有什么不同吗? onChange从MainActor ( SwiftUI视图) 调用。 这两个选项都在主线程上执行。 在主线程和 UI 上发挥的性能有什么区别吗? ...
[英]Calling UIViewController dismiss and/or present in an async context?
根据Calling Objective-C APIs Asynchronously从 Objective-C 导入的满足特定要求的方法“作为两种方法导入”,其中一种是async的,而不是具有完成块。 如果该方法有多个参数,并且最后一个参数的选择器部分是以下之一,Swift 将该方法作为异步方法导入 ...
[英]Shorthand for calling Concurrency within Combine sink?
我正在订阅 Combine 发布者,但经常在.sink中调用并发任务。 有没有更方便的方法来做到这一点?import _Concurrency import Combine import Foundation import PlaygroundSupport var cancellable = ...
[英]How to avoid function being executed in parallel?
我正在使用来自Core Location的locationManager(_:didVisit:)来在用户到达或离开某个位置时收到通知。 但是,我发现回调有时可能会被调用两次。 因为我会将收到的数据保存到核心数据中,所以我想在极少数情况下删除重复项。 我在 Swift 中没有并发经验。我该怎么做才 ...
[英]Why are nested tasks not canceled when they parent task is cancelled?
我需要取消所有嵌套任务 我尝试取消它们的父级但没有任何反应所有嵌套任务继续运行。private var observationTask: Task<Void, Never>? ... observationTask = Task { Task { for awa ...