swift-concurrency Questions

2

Solved

I have an ObservableObject which can do a CPU-bound heavy work: import Foundation import SwiftUI @MainActor final class Controller: ObservableObject { @Published private(set) var isComputing: Boo...
Corm asked 16/3, 2022 at 16:5

1

Currently, I have the following code class ShopViewController: UIViewController { @IBAction func buy(_ sender: Any) { Task { // Will run in main thread, because ShopViewController is // marke...
Hawthorn asked 25/4, 2022 at 15:6

1

Solved

I would like to understand if it's needed to declare the Task itself as MainActor considering the following block of code. func login() { Task { [weak self] in let result = await self?.loginServi...
Lipoprotein asked 19/4, 2022 at 10:33

2

Solved

Okay so we all know that in traditional concurrency in Swift, if you are performing (for example) a network request inside a class, and in the completion of that request you reference a function th...
Strata asked 3/4, 2022 at 18:49

1

When creating a Task in a task-less context (e.g. from some random AppKit code), is there a difference between creating a detached or a regular task? For example, calling Task.detached or Task.ini...
Aborticide asked 22/3, 2022 at 16:43

1

Solved

I am trying to perform a series of network requests and would like to limit the number of concurrent tasks in the new Swift Concurrency system. With operation queues we would use maxConcurrentOpera...
Dittmer asked 3/2, 2022 at 18:17

1

Solved

I'm new (like most everyone, I suppose) to Swift concurrency, and I'm running into a compiler error I don't know what to do with. struct Thing { var counter = 0 mutating func increment() async {...
Transgression asked 17/1, 2022 at 22:7

1

Solved

I am listening for changes of a publisher, then fetching some data asynchronously in my pipeline and updating the view with the result. However, I am unsure how to make this testable. How can I bes...
Groceryman asked 12/1, 2022 at 19:37

3

Solved

In WWDC 2021 video, Protect mutable state with Swift actors, they provide the following code snippet: actor ImageDownloader { private var cache: [URL: Image] = [:] func image(from url: URL) asyn...
Belton asked 5/1, 2022 at 0:30

1

I’m currently migrating my app to use the concurrency model in Swift. I want to serialize Tasks to make sure they are executed one after the other (no paralellism). In my use case, I want to listen...

1

Solved

Integrating actors with existing code doesn't really seem to be as simple as Apple wants you to believe. Consider the following simple actor: actor Foo { var value: Int = 0 } Trying to access thi...
Poetess asked 13/12, 2021 at 8:58

1

Solved

Here is the code in Apple developer document。 let url = URL(string: "https://example.com")! @State private var message = "Loading..." var body: some View { Text(message) .tas...
Geek asked 10/12, 2021 at 16:11

1

Solved

Problem: I have the following function which shows a warning No calls to throwing functions occur within 'try' expression Questions: Why is this warning shown? (The code inside the task throws an ...
Checkrow asked 23/11, 2021 at 9:38

2

In this case the async function reads a file and returns the parsed contents. In my view I want to load the contents off of the main thread, and then update the view once complete. I've used this p...
Electroshock asked 28/10, 2021 at 12:17

1

Solved

I have a callback based API. func start(_ completion: @escaping () -> Void) I'd like to write an async/await wrapper on top of that API, deferring the implementation to original callback based ...
Thaliathalidomide asked 9/11, 2021 at 18:5

2

Solved

I have an existing debouncer utility using DispatchQueue. It accepts a closure and executes it before the time threshold is met. It can be used like this: let limiter = Debouncer(limit: 5) var valu...

1

Solved

I would like to have some sort of global variable that is synchronized using @MainActor. Here's an example struct: @MainActor struct Foo {} I'd like to have a global variable something like this: ...
Abstractionism asked 21/9, 2021 at 5:47

1

I have a situation where I'm creating many Cocoa objects in a loop using async/await, and the memory spikes because the objects are only released when the loop is over (instead of every iteration)....
Comprehensible asked 28/8, 2021 at 19:11

1

Solved

I need to use my own custom error enum in tasks that I create: enum MyError: Error { case someError } var myTask: Task<MyModel, MyError> = Task { () throws -> MyModel in // in case of a...
Hypoacidity asked 17/8, 2021 at 12:24

1

I try to check the actor's behavior. This is a new feature provided by Swift5.5. I've created a playground with an example code from the official documentation swift.org: import Foundation acto...
Incipient asked 13/6, 2021 at 10:0

1

Solved

My intent is to understand the “cooperative thread pool” used by Swift 5.5’s async-await, and how task groups automatically constrain the degree of concurrency: Consider the following task group co...

1

Solved

I recently saw that Swift had introduced concurrency support with the Actor model in Swift 5.5. This model enables safe concurrent code to avoid data races when we have a shared, mutable state. I w...
Coprolite asked 8/6, 2021 at 19:23

© 2022 - 2025 — McMap. All rights reserved.