hmlongco / Resolver

Swift Ultralight Dependency Injection / Service Locator framework
MIT License
2.14k stars 187 forks source link

Actor Based Register Method #165

Closed Zoha131 closed 1 year ago

Zoha131 commented 1 year ago

When I try to register an object which is marked as @MainActor then it gives the following error

Call to main actor-isolated initializer 'init()' in a synchronous nonisolated context

Is it possible to support this kind of register method?

hmlongco commented 1 year ago

This was discussed at some length in Factory. The two solutions are to a) don't do that, or b) mark your init as nonisolated.

The best solution is a) don't mark the entire class as @MainActor. It's usually not necessary. Just mark the async functions @MainActor as needed.

https://github.com/hmlongco/Factory/issues/21