If I'm in a callback function that is called by library code, and I need to return a value from a MainActor-isolated property, how can I get at it?
As a callback, I basically have no guarantees about what thread it will get called on. If I know I'm not on the main thread, I can do DispatchQueue.main.sync { ··· } and the compiler is happy.
But if I verify that I'm running on the main thread, how do I convince the compiler that I'm in the MainActor's context and can safely access isolated stuff?
I can't fire off a task with e.g. Task { @MainActor in ··· } because I have to wait for the task to finish and get me a result, and since I'm already on the main thread where that task would run, that's deadlock.
My specific case is that I'm working with libgit2, where I have things like git_remote_callbacks.git_indexer_progress_cb in which I can return a negative value to cancel.
If sorry but I’m not familiar with that library. I was hoping to get a model of the problem, something that abstracts away the specific library details but is concrete enough to discuss the mechanics.
This roundabout technique lets you (unsafely) tell Swift that you're on the MainActor already and would like to run code on it. (The specific context I needed this was a pre-concurrency protocol that I knew was @MainActor but not annotated as such, so I had to conform to it with nonisolated methods.)