Post

Replies

Boosts

Views

Activity

Function called isolated on MainActor via an isolated parameter are not considered run on the MainActor
If I try to compile the following, I get a compilation error: import Foundation func isolatedPrint<A : Actor>(on actor: isolated A) { print("hello") } Task{ @MainActor in isolatedPrint(on: MainActor.shared) } The error: toto.swift:9:2: error: expression is 'async' but is not marked with 'await' isolatedPrint(on: MainActor.shared) ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ await toto.swift:9:2: note: calls to global function 'isolatedPrint(on:)' from outside of its actor context are implicitly asynchronous isolatedPrint(on: MainActor.shared) ^ I don’t understand why the compiler does not detect the function is called on the MainActor via the actor parameter.
1
0
430
May ’24
Actor ‘self’ can only be passed ‘inout’ from an async initializer
Context Let say we have an actor, which have a private struct. We want this actor to be reactive, and to give access to a publisher that publishes a specific field of the private struct. The following code seems to work. But I do have questions. public actor MyActor { private struct MyStruct { var publicField: String } @Published private var myStruct: MyStruct? /* We force a non isolated so the property is still accessible from other contexts w/o await in other modules. */ public nonisolated let publicFieldPublisher: AnyPublisher<String?, Never> init() { self.publicFieldPublisher = _myStruct.projectedValue.map{ $0?.publicField }.eraseToAnyPublisher() } } Question 1 & 2 In the init, I use _myStruct.projectedValue, which should be strictly equivalent to $myStruct. Except the latter does not compile. We get the following error: 'self' used in property access '$myStruct' before all stored properties are initialized Why? AFAICT myStruct should be init’d to nil, so where is the problem? And why does the former do compile? Question 3 With _myStruct.projectedValue, I get a warning at compile-time: Actor 'self' can only be passed 'inout' from an async initializer What does that mean? Is it possible to get rid of this warning? Is it “dangerous” (can this cause issues later)? Thanks!
1
0
2.0k
Nov ’21
Why is URLSession’s did receive response delegate method asynchronous?
I don’t mind it, but I don’t understand it. The delegate method for a data task when the session first receives an URL response is asynchronous: it gives a completion handler that must be called in order to provide the response disposition (or is async if _Concurrency is available). What kind of check on the response can be done that justify the response disposition be sent asynchronously?
1
0
626
Nov ’21
Background App Refresh and Significant Location Changes Tracking
Hello, I’m writing an app that records the position of the user at all time (when he requests it!). I have two questions. I’ve read the doc, and found this Note: When a user disables the Background App Refresh setting either globally or for your app, the significant-change location service doesn’t relaunch your app. Further, while Background App Refresh is off an app doesn’t receive significant-change or region monitoring events even when it's in the foreground. and this Important: A user can explicitly disable background capabilities for any app. If a user disables Background App Refresh in the Settings app—either globally for all apps or for your app in particular—your app is prevented from using any location services in the background. You can determine whether your app can process location updates in the background by checking the value of the backgroundRefreshStatus property of the UIApplication class. First question: Why my app does not appear in Background App Refresh even though I use the significant location change API? Second question: I tested the doc’s statements, and found out my app was revived after being killed when a significant location change occurred. Does anybody know if the doc is out-of-date, or if it never was accurate? (Or if I misunderstood something!)
2
1
3.3k
Jul ’20