[英]SwiftUI/ Combine Listen to multiple publishers inside an ObservableObject
I have an ObservableObject
which inside has two more ObservableObjects
.我有一个ObservableObject
里面有两个ObservableObjects
。 Each of these two ObservableObject
has one or more @Published
properties inside.这两个ObservableObject
每一个都有一个或多个@Published
属性。 My solution works but I'm 100% sure there must be another way.我的解决方案有效,但我 100% 肯定必须有另一种方法。 I have to copy/ paste everything in order to make it work.我必须复制/粘贴所有内容才能使其正常工作。 I tried two put those two publishers in the View but then I need to pass a lot of params to the child view.我尝试了两个将这两个发布者放在视图中,但随后我需要将很多参数传递给子视图。 Any ideas how to simplify this?任何想法如何简化这个?
class PackageService: ObservableObject {
@Published var package: Package
@Published var error: Error?
@Published var distance: Double?
@Published var expectedTravelTime: String?
@Published var amount: Int = 0
@Published var cost: Double = 0
@Published var annotations = [MKPointAnnotation]()
@Published var isCalculating = true
@Published var route: MKRoute?
var amountService = AmountService()
var routeService = RouteService()
private var cancellables = Set<AnyCancellable>()
init(package: Package) {
self.package = package
routeService.calcDirections(source: package.source.toCLLocationCoordinate2D, destination: package.destination.toCLLocationCoordinate2D)
routeService.$route.sink { [self] route in
self.route = route
amountService.calc(distance: route?.distance)
calc(route: route)
}
.store(in: &cancellables)
routeService.$isCalculating.sink { [self] isCalculating in
self.isCalculating = isCalculating
}
.store(in: &cancellables)
routeService.$error.sink { [self] error in
self.error = error
}
.store(in: &cancellables)
amountService.$amount.sink { [self] amount in
self.amount = amount
}
.store(in: &cancellables)
amountService.$cost.sink { [self] cost in
self.cost = cost
}
.store(in: &cancellables)
amountService.$error.sink { [self] error in
self.error = error
}
.store(in: &cancellables)
}
Seems like the PackageService
is just a thin wrapper around two other services.似乎PackageService
只是其他两个服务的薄包装器。
It would make sense to use computed properties instead of subscribing and publishing a change for each property:使用计算属性而不是订阅和发布每个属性的更改是有意义的:
var isCalculating: Bool { routeService.isCalculating }
var amount: Int { amountService.amount }
// etc...
and to subscribe once in order to signal changes to the view:并订阅一次以发出视图更改的信号:
routeService.objectWillChange
.merge(with: amountService.objectWillChange)
.sink(receiveValue: self.objectWillChange.send)
.store(in: &cancellables)
Also, consider packaging the various properties in a single data model, eg:另外,考虑将各种属性打包在一个数据模型中,例如:
struct AmountModel {
var amount: Int
var cost: Double
}
and have the AmountService
have a single property.并让AmountService
有一个属性。 This could be a Result<AmountModel, Error>
to capture either the data or the error, or, if you need, include the Error
as a property.这可能是一个Result<AmountModel, Error>
来捕获数据或错误,或者,如果需要,将Error
作为属性包含在内。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.