I was thinking about creating a DI using Actors to ensure data race safety and was exploring the idea. But this error was fired in my face saying "Cannot form key path to actor-isolated property 'networkProvider' " . I don't understand the error and if it's not allowed to write keyPath for actors ?
Here is the code:
public actor InjectedValues {
/// This is only used by the subscript to access the computed property's in the ``InjectedValues`` extension.
private static var current = InjectedValues()
/// This is a static subscript to read and update the currentValue of the ``InjectionKey``.
public static subscript<K>(key: K.Type) -> K.Value where K: InjectionKey {
get { key.currentValue }
set { key.currentValue = newValue }
}
/// This is a static subscript to reference and update the dependencies directly.
// public static subscript<T>(_ keyPath: ReferenceWritableKeyPath<InjectedValues, T>) -> T {
// get async { current[keyPath: keyPath] }
// }
public static func update<T>(_ keyPath: ReferenceWritableKeyPath<InjectedValues, T>, _ value: T) async {
self.current[keyPath: keyPath] = value
}
}
// Usage
await InjectedValues.update(\.networkProvider, NetworkProviderMock()) // Error: Cannot form key path to actor-isolated property 'networkProvider