In my current project I have a client identifier that I use in a bunch of places. Let's say it is defined like this:
enum E {
static let clientIdentifier: String = makeClientIdentifier()
}
Sadly, makeClientIdentifier()
needs to run on the @MainActor
because it accesses UIDevice
. I'm pretty sure I could come up with a way to pass the info around in another way, and I probably should do it anyway, because the values returned from UIDevice are a side effect out of my control, but I still feel like I should ask anyway, even if its just to learn something.
I could use MainActor.assumeIsolated
inside makeClientIdentifier()
and document, that the first use must occur on the main actor but this seems fragile. Same with making clientIdentifer
privately a nonisolated(unsafe)
var
that expects to be "set up" with an extra function that is called as early as possible on the main actor -> Yikes!
Given that the operation is pretty cheap: Is there any other way to instantiate a value on the main actor that is otherwise perfectly sendable and assign it to a static let?