Global Sendable constants that need to be instantiated on the MainActor

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?

1 Like

Someone already has already filed an issue to the Apple regarding UIDevice: Main actor-isolated property error in non isolated context - #11 by yasali

I’m not sure how realistic this change to land though, but it definitely creates a lot of confusion and really questionable if it should be main actor isolated.

1 Like