我有这个场景有几个环境对象:
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
// Get the managed object context from the shared persistent container.
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
// Create the SwiftUI view and set the context as the value for the managedObjectContext environment keyPath.
// Add `@Environment(\.managedObjectContext)` in the views that will need the context.
let contentView = MainTabView()
//Inject Database repository
.environmentObject(DatabaseRepository())
.environmentObject(UserRepository())
.environmentObject(StockRepository(api: AlphaVantageAPI()))
.environment(\.managedObjectContext, context)
// Use a UIHostingController as window root view controller.
if let windowScene = scene as? UIWindowScene {
let window = UIWindow(windowScene: windowScene)
window.rootViewController = UIHostingController(rootView: contentView)
self.window = window
window.makeKeyAndVisible()
}
}
在我的 MainTabView 中,我有一个这样创建的选项卡:
struct MainTabView: View {
@EnvironmentObject var database: DatabaseRepository
@EnvironmentObject var userRepository: UserRepository
@EnvironmentObject var stockRepository: StockRepository
var viewModel = MainTabViewModel()
init() {
UITabBar.appearance().barTintColor = UIColor(named: "primary")
}
var body: some View {
TabView {
HomeView(viewModel: HomeViewModel(databaseRepository: database,
userRepository: userRepository,
stockRepository: stockRepository))
.tabItem {
Image("account_balance_wallet")
.renderingMode(.template)
Text("Home")
}.tag(0)
...
}.accentColor(.white)
}
}
在我的 HomeVM 中,我有以下代码:
class HomeViewModel: ObservableObject {
@Published var watchingVMs = [EquityPreviewCellViewModel]()
private let databaseRepository: DatabaseRepositoryProtocol
private let userRepository: UserRepositoryProtocol
private let stockRepository: StockRepositoryProtocol
init(databaseRepository: DatabaseRepositoryProtocol,
userRepository: UserRepositoryProtocol,
stockRepository: StockRepositoryProtocol) {
self.databaseRepository = databaseRepository
self.userRepository = userRepository
self.stockRepository = stockRepository
self.bind()
}
func bind() {
let allorders = databaseRepository
.allOrder(userID: userRepository.userID).share()
allorders
.assertNoFailure()
.compactMap({orders in
return orders?.reduce([Order](), { finalOrders, nextOrder in
var orders = [Order](finalOrders)
if !finalOrders.contains(where: { $0.symbol == nextOrder.symbol }) {
orders.append(nextOrder)
}
return orders
}).map({EquityPreviewCellViewModel(order: $0,
stockRepository: self.stockRepository,
userRepository: self.userRepository,
dataBaseRepository: self.databaseRepository)})
})
.receive(on: DispatchQueue.main)
.assign(to: &self.$watchingVMs)
我启动我的应用程序时发生了崩溃:
线程 1:EXC_BAD_INSTRUCTION(代码=EXC_I386_INVOP,子代码=0x0)
我想我用得不好,environmentObject
但我不明白为什么。