AsyncBytes not hopping back to original queue after delivering URLResponse

A friend told me this might be a bug and to mention it here.

I'm creating a new async context using SwiftUI's .task { } modifier, which is asserted to be on the main queue, then stays on the main queue after the method call, but then isn't on the main queue after getting the URLResponse back from bytes(for:):

1 Like

Can you share a more compete snippet / picture?

Where is this startDownload function defined; is it actually guaranteed to be on the main actor or just happened to be so -- if the latter, then there is no guarantee where it'll hop back.

(Can you show the type declaration of where this function is defined in?)

It's just a regular function in a SwiftUI View struct, you're seeing the only place it's defined and the only place it's called.

Let me play with making it the startDownload function @MainActor. I'll post more when I know more!

Only functions and/or types annotated with @MainActor are actually guaranteed to run on the main actor. Otherwise it's no-one's business "where" the actor executes. If it would be in a type that is @MainActor I'd expect it to hope back indeed.

1 Like

Yeah, that fixes it. The function executes as expected and none of the preconditions are triggered. Thanks for the help!

1 Like

btw, considering that wasn't the main queue here but some other queue: what'd be wrong with hopping back to the original queue? is it because we don't want (why?) to retain the current queue to ensure it's still around by the time we need to hop back to it or is there another reason?

Terms of Service

Privacy Policy

Cookie Policy