I'm playing with SwiftUI and would like to abstract an @EnvironmentObject. The goal is to switch from a production BindableObject to a fake one (testing / working local ...)
First I just declared a protocol:
protocol FetcherInterface: BindableObject {
associatedtype T
var didChange: PassthroughSubject<[T], Never> { get set }
var values: [T] { get set }
}
Then I can wrote a network root class conforming to FetcherInterface:
open class NetworkFetcher<T: Decodable>: FetcherInterface {
public var didChange = PassthroughSubject<[T], Never>()
internal var values: [T] = [T]() {
didSet {
DispatchQueue.main.async {
dump("did set network values \(self.values)")
self.didChange.send(self.values)
}
}
}
internal func loadAsync(values: [T]) {
DispatchQueue.global().asyncAfter(deadline: .now() + 1) {
self.values = values
}
}
}
I can now have a child class like this one:
final class PlacesNetworkFetcher: NetworkFetcher<Place>, PlacesQueryInterface {
func loadPlacesFromCountryCode(_ countryCode: String) {
self.loadAsync(values: [Place(id: UUID(), name: "London")])
}
}
with PlacesQueryInterface:
protocol PlacesQueryInterface {
func loadPlacesFromCountryCode(_ countryCode: String)
}
extension PlacesQueryInterface where Self: FetcherInterface {}
When I want to use all of this in my ContentView.swift Xcode never end compilation.
Looks like environment object is causing this:
@EnvironmentObject var placesQueryInterface: PlacesQueryInterface
Do you have an idea why ?
edit: I put a project skeleton if you want to test
