.onPreview
not working when having set up defaultScope
for whole SharedContainer
#118
-
Hi! I would like to ask about the behaviour of For the debug purposes I'm having a import Factory
import SwiftUI
struct ContentView: View {
@StateObject private var viewModel = ContentViewModel()
var body: some View {
VStack {
Text(viewModel.dataString)
}
.padding()
.onAppear(perform: viewModel.reloadData)
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
final class ContentViewModel: ObservableObject {
@Published var dataString: String = ""
@Injected(\DataServiceContainer.dataService) private var dataService
func reloadData() {
Task { @MainActor in
dataString = dataService.fetchString()
}
}
} And having a protocol DataService {
func fetchString() -> String
}
class DataServiceImpl: DataService {
func fetchString() -> String {
"PRODUCTION"
}
}
class DataServiceMock: DataService {
func fetchString() -> String {
"DEBUG"
}
} While setting up Preview showing mocked data: import Factory
final class DataServiceContainer: SharedContainer {
static let shared = DataServiceContainer()
let manager = ContainerManager()
var dataService: Factory<DataService> {
self { DataServiceImpl() }
.onPreview { DataServiceMock() }
.singleton
}
} Preview showing real data: import Factory
final class DataServiceContainer: SharedContainer {
static let shared = DataServiceContainer()
var manager: ContainerManager {
let manager = ContainerManager()
manager.defaultScope = .singleton
return manager
}
var dataService: Factory<DataService> {
self { DataServiceImpl() }
.onPreview { DataServiceMock() }
}
} Is there anything I'm missing out while setting up scope for the whole container? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Not sure if this is the or just a problem, but manager stores caches, registrations, and options for the container. The following code basically creates a new manager every single time it's needed. var manager: ContainerManager {
let manager = ContainerManager()
manager.defaultScope = .singleton
return manager
} At the very least that should be... var manager: ContainerManager = {
let manager = ContainerManager()
manager.defaultScope = .singleton
return manager
}() |
Beta Was this translation helpful? Give feedback.
Not sure if this is the or just a problem, but manager stores caches, registrations, and options for the container. The following code basically creates a new manager every single time it's needed.
At the very least that should be...