swift-concurrency Questions

6

Solved

Update The accepted answer did not directly answer the original question, but helped resolve the underlying issue I tried to solve: I wanted to map an AsyncStream (which is an AsyncSequence) into a...
Tager asked 20/8, 2022 at 9:58

2

Consider the following, relatively simple Swift program: import Foundation func printContext(function: String = #function, line: Int = #line) { print("At \(function):\(line): Running on \(Th...
Kalgan asked 31/8, 2022 at 20:2

3

Solved

Context I have a Mac app that uses the old QuickLook protocols: QLPreviewPanelDataSource and QLPreviewPanelDelegate. You select a row in an NSTableView, hit the spacebar, and you get the QuickLook ...
Transverse asked 11/9, 2023 at 22:39

8

I'm bridging the sync/async worlds in Swift and doing incremental adoption of async/await. I'm trying to invoke an async function that returns a value from a non async function. I understand that e...
Enounce asked 2/2, 2022 at 20:35

3

I've been watching Apple's concurrency talks from WWDC21 as well as reading a ton of articles on Apple's concurrency updates; however, I cannot wrap my head around one thing: Why do people provide ...
Milkwort asked 14/12, 2021 at 19:52

2

Solved

I have a simple piece of code: struct ContentView: View { var body: some View { Text("Hello world!") .task { await myAsyncFunc() } } private func myAsyncFunc() async {} } This co...
Helices asked 27/11, 2021 at 5:0

3

Solved

I have an actor: actor StatesActor { var job1sActive:Bool = false ... } I have an object that uses that actor: class MyObj { let myStates = StatesActor() func job1() async { myStates....
Styrax asked 20/9, 2021 at 18:43

6

Solved

I have an ObservableObject class and a SwiftUI view. When a button is tapped, I create a Task and call populate (an async function) from within it. I thought this would execute populate on a backgr...
Thapsus asked 12/4, 2022 at 4:38

1

Solved

I try to figure out is this approach thread safe if getStream() and update(value: ...) will be called on difference thread simultaneously? final class SomeNotifier { static let shared = SomeNotifi...
Kappa asked 27/3, 2023 at 11:28

3

Solved

I have watched Explore structured concurrency in Swift video and other relevant videos / articles / books I was able to find (swift by Sundell, hacking with swift, Ray Renderlich), but all examples...
Commissariat asked 14/4, 2022 at 12:46

1

Solved

I was trying out the new async/await pattern in swift and I came accross something which I found it confusing. struct ContentView: View { var body: some View { Text("Hello World!") .ta...
Yeah asked 18/2, 2023 at 22:0

2

Solved

In the introductory section of the Concurrency chapter of "The Swift Programming Language" I read: When an asynchronous function resumes, Swift doesn’t make any guarantee about which thr...
Thermoluminescence asked 30/1, 2023 at 13:58

1

I have a class named Person, Which is annotated with @MainActor. But as I try to create an instance of this mainactor class it gives an error. "Call to main actor-isolated initializer 'init(f...
Weinhardt asked 21/12, 2022 at 10:36

2

Solved

Context In a Mac app built with Swift 5.x and Xcode 14, I have a controller object. This object has several @Published properties that are observed by SwiftUI views, so I have placed the object on ...
Silvereye asked 8/12, 2022 at 10:42

2

Solved

How do I use the new Swift 5.5 await keyword to wait for a duration of time? Normally, with completion handlers, you would have something like this by using DispatchQueue's asyncAfter(deadline:exec...
Sternson asked 9/8, 2021 at 16:9

1

I want to click a button in SwiftUI that will trigger a JSON encoding action. This action is time consuming thus I need it to be async. I have already tried two solutions but they do not work. One ...
Averill asked 2/4, 2022 at 17:4

2

Solved

I'm trying to conform a class to Sendable. I have some mutable stored properties which are causing issues. However, what I can't understand is that a MainActor isolated property doesn't allow my cl...
Gemmation asked 15/11, 2022 at 17:43

1

Is there any difference between: Task { await MainActor.run { ... } } and Task { @MainActor in ... }
Gathering asked 22/9, 2022 at 10:40

0

Consider the following code: class Cat { var name = "Tom" } class Globals { var cat = Cat() } let glob = Globals() func one () { Task {glob.cat.name="Max"} // Expected War...
Amazed asked 23/10, 2022 at 12:25

2

Solved

I want to keep firing a function 5 seconds after it completes. Previously I would use this at the end of the function: Timer.scheduledTimer(withTimeInterval: 5, repeats: false) { self.function() } ...
Cristacristabel asked 11/8, 2021 at 15:9

1

Solved

I need to cancel all nested tasks I try to cancel their parent but nothing happens all nested tasks keep running. private var observationTask: Task<Void, Never>? ... observationTask = Task { ...
Fabrice asked 7/10, 2022 at 5:49

1

I am trying to add an async do/catch/defer action to a UIButton. However, if I just call a method in the defer block, I get Call to main actor-isolated instance method XXX in a synchronous nonisola...
Pantalets asked 13/12, 2021 at 4:9

1

Solved

We can create a @MainActor Task like this: Task { @MainActor in print("hi") } But also with @MainActor(unsafe) like this: Task { @MainActor(unsafe) in print("hi") } What's t...
As asked 13/12, 2021 at 12:13

1

Solved

I've a document based application that uses a struct for its main data/model. As the model is a property of (a subclass of) NSDocument it needs to be accessed from the main thread. So far all good....
Neologize asked 18/7, 2022 at 17:45

1

Solved

I have a class Artist, and below is a function to create a new artist and insert it to my library array. Because insertToArtists(artist:) is a asynchronous function, I have to wrap it in Task. But ...
Commandeer asked 11/5, 2022 at 9:23

© 2022 - 2025 — McMap. All rights reserved.