Replies: 1 comment 4 replies
-
Hey @rrozenv, I believe the plan is to seperate There are probably a few ways to achieve this at the moment though. One that I've used before is to create a property wrapper that wraps around @propertyWrapper
public struct SharedDependency<Value>: Sendable {
@Dependency private var value: Value
public init(
_ keyPath: KeyPath<DependencyValues, Value>,
file: StaticString = #file,
fileID: StaticString = #fileID,
line: UInt = #line
) {
self._value = Dependency(keyPath, file: file, fileID: fileID, line: line)
}
public var wrappedValue: Value {
DependencyValues.withValue(\.self, DependencyValues.shared) { self.value }
}
} And the shared values could look something like this: extension DependencyValues {
private static let lock = NSRecursiveLock()
private static var _shared = DependencyValues()
public static var shared: DependencyValues {
_read {
Self.lock.lock()
defer { Self.lock.unlock() }
yield Self._shared
}
_modify {
Self.lock.lock()
defer { Self.lock.unlock() }
yield &Self._shared
}
}
} This isn't what I use at the moment so I haven't really double checked the above. It's just similar to what I used to do. You can also add an extension to extension Dependency {
@available(*, deprecated, message: "Use SharedDependency until swift-dependencies is released")
public init(_ keyPath: KeyPath<DependencyValues, Value>) {
self.init(keyPath, file: #file, fileID: #fileID, line: #line)
}
} |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I know the primary use case for
Dependency
is in TCA, but I'm wondering if it's currently possible to use outside of TCA.My goal is to be able to easily override certain dependencies for a class instance like
FeatureModel
. For example, to generate a preview forFeatureView
which depends onFeatureModel
I'd like to do something like this:Obviously this doesn't work as the overridden dependency only lives for the scope of
operation
(also the compiler has trouble interpreting this sort of code).I also attempted to more crudely just overwrite the global dependency through
DependencyValues._current
:This doesn't work either as
'_current' is a get-only property
.Having the ability to modify
_current
seems like a potential solution for this specific use case but could of course have many unintended consequences. So I'm wondering if anyone has any ideas on how to get something like this to work?Thanks in advance!
*The issue I'm describing is similar to the one noted here: #1533 but the use case I'm describing has some key differences
Beta Was this translation helpful? Give feedback.
All reactions