当ObservableObject的所有属性更新时,将通知SwiftUI

时间:2020-02-09 11:57:37

标签: swift mvvm swiftui xcode11 combine

我有一个ObservableObject


class CurrentPosition: ObservableObject {
    @Published var northEast = CLLocationCoordinate2D()
    @Published var southWest = CLLocationCoordinate2D()


    init(northEast: CLLocationCoordinate2D = CLLocationCoordinate2D(), southWest: CLLocationCoordinate2D = CLLocationCoordinate2D()) {
        self.northEast = northEast
        self.southWest = southWest
    }

    func updateCoordinates(from mapView: MKMapView) {
        self.northEast = mapView.northEastCoordinate
        self.southWest = mapView.southWestCoordinate
    }
}

我想从MapView更新的

struct MapView: UIViewRepresentable {
    @Binding var weatherStations: [WeatherStation]
    @Binding var selectedWeatherStation: WeatherStation?

    @EnvironmentObject var currentPosition: CurrentPosition

    func makeUIView(context: Context) -> MKMapView {
        let map = MKMapView()
        map.showsUserLocation = true
        map.delegate = context.coordinator
        return map
    }

    func updateUIView(_ uiView: MKMapView, context: Context) {
        updateAnnotations(from: uiView)
    }

    func makeCoordinator() -> Coordinator {
        Coordinator(self)
    }


    final class Coordinator: NSObject, MKMapViewDelegate {
        var control: MapView

        init(_ control: MapView) {
            self.control = control
        }

        func mapView(_ mapView: MKMapView, regionDidChangeAnimated animated: Bool) {
            control.currentPosition.updateCoordinates(from: mapView)
        }

    }
}

并将其传递给AppState对象

 MapView(weatherStations: $appState.appData.weatherStations,
                        selectedWeatherStation: $selectedWeatherStation).environmentObject(self.currentPosition)
                    .edgesIgnoringSafeArea(.vertical).onReceive(currentPosition.objectWillChange) { () in
                        self.appState.currentPosition = self.currentPosition
                }}

这又将其传递给我的API客户端

class AppState: ObservableObject {
    @Published var appData = AppData(weatherStations: [WeatherStation]())
    var currentPosition: CurrentPosition! {
        didSet {
            api.currentPosition = self.currentPosition
        }
    }
}

我的问题是,每次我的CurrentPosition属性之一被更新时,它就会通过,这意味着northEast更新时而不是southWest被更新时才通过。

当两个都完成更新后,我如何才能只传递一次?

1 个答案:

答案 0 :(得分:1)

简单的方法是使用辅助struct将其折叠为一个属性,如下所示:

struct MapCoordinateSpan {
  let northEast: CLLocationCoordinate2D
  let southWest: CLLocationCoordinate2D
}

并修改您的CurrentPosition如下所示:

class CurrentPosition: ObservableObject {
    @Published var span: MapCoordinateSpan

    init(northEast: CLLocationCoordinate2D = CLLocationCoordinate2D(), southWest: CLLocationCoordinate2D = CLLocationCoordinate2D()) {
        self.span = MapCoordinateSpan(northEast: northEast, southWest: southWest)
    }

    func updateCoordinates(from mapView: MKMapView) {
        self.span = MapCoordinateSpan(northEast: mapView.northEastCoordinate, southWest: mapView.southWestCoordinate)
    }
}

然后绑定到span属性,并从那里拉出坐标。