Ios 当@State、@Binding或@EnvironmentObject发生更改时,不会调用UIViewRepresentable.UpdateUIVIView(U3;:上下文:)

Ios 当@State、@Binding或@EnvironmentObject发生更改时,不会调用UIViewRepresentable.UpdateUIVIView(U3;:上下文:),ios,swift,state,swiftui,uiviewrepresentable,Ios,Swift,State,Swiftui,Uiviewrepresentable,当此视图及其父视图所依赖的ObserveObject发生更改时,不会调用UIViewRepresentable.updateUIView 我有一个带有“@State var locationManager:locationManager”的游戏视图,它作为绑定传递给我的地图视图。MapView符合UIViewRepresentable协议。LocationManager符合ObservieObject,并具有以下符合性相关代码: var didChange=PassthroughSubject(

当此视图及其父视图所依赖的ObserveObject发生更改时,不会调用UIViewRepresentable.updateUIView

我有一个带有“@State var locationManager:locationManager”的游戏视图,它作为绑定传递给我的地图视图。MapView符合UIViewRepresentable协议。LocationManager符合ObservieObject,并具有以下符合性相关代码:

var didChange=PassthroughSubject()
var lastKnownLocation:CLLocation{
didSet{
//将更新传播给观察者。
发送(自我)
打印(“已完成向观察者传播lastKnownLocation。”)
}  
}
我想每次LocationManager.lastKnownLocation更改时,GameView和MapView都必须更新。实际上,我只看到在退出应用程序的每个主页按钮时调用MapView.UpdateUI()。此时,控件进入updateUIView(),当我再次打开应用程序(不是编译安装运行)时,我会得到更新。在GameView()出现后不久也会发生这种情况

我对SwiftUI工作原理的理解是错误的还是错误的?我怎样才能把它做好

struct GameView:View{
@状态变量locationManager:locationManager
变量体:某些视图{
地图视图(locationManager:$locationManager)
}
}
结构映射视图:UIViewRepresentable{
@绑定变量locationManager:locationManager
func makeUIView(context:context)->GMSMapView{…}
func updateUIView(uMapView:GMSMapView,context:context){…}
}
类LocationManager:NSObject、CLLocationManagerDelegate、ObservableObject{
var didChange=PassthroughSubject()
var LastKnown位置:CLLocation{
迪塞特{
//将更新传播给观察者。
发送(自我)
打印(“已完成向观察者传播lastKnownLocation。”)
}
}
...
}

我希望每次tim LocationManager.lastKnownLocation更改时,都会调用MapView.updateUIView()。实际上,只有当我按“主页”按钮退出应用程序(然后再次输入)时,才会发生呼叫。

像这样更改地图视图

struct MapView: UIViewRepresentable {
  @State var locationManager = LocationManager()

  func makeUIView(context: Context) -> GMSMapView{
    let view = GMSMapView(frame: .zero)
    ...
    willAppear(context)
    return view
  }

  func updateUIView(_ mapView: GMSMapView, context: Context) { ... }

  func makeCoordinator() -> MapView.Coordinator {
    return Coordinator()
  }

  static func dismantleUIView(_ uiView: GMSMapView, coordinator: MapView.Coordinator) {
    coordinator.cancellable.removeAll()
  }

  class Coordinator {
    var cancellable = Set<AnyCancellable>()
  }
}

extension MapView {
  func willAppear(_ context: Context) {
    locationManager.didChange.sink{
      print("location: \($0)")
    }.store(in: &context.coordinator.cancellable)

    locationManager.startUpdating()
  }
}
结构映射视图:UIViewRepresentable{ @状态变量locationManager=locationManager() func makeUIView(上下文:context)->GMSMapView{ let view=GMSMapView(帧:.0) ... 将出现(上下文) 返回视图 } func updateUIView(uMapView:GMSMapView,context:context){…} func makeCoordinator()->MapView.Coordinator{ 返回协调员() } 静态函数拆卸视图(uiView:GMSMapView,coordinator:MapView.coordinator){ coordinator.cancelable.removeAll() } 班级协调员{ var cancelable=Set() } } 扩展地图视图{ func将出现(ucontext:context){ locationManager.didChange.sink{ 打印(“位置:\($0)”) }.store(在:&context.coordinator.Cancelable中) locationManager.startUpdating() } }
我认为这个方法
locationManager.startUpdating()
locationManager.StopUpdate()
需要调用另一个类。

像这样更改MapView

struct MapView: UIViewRepresentable {
  @State var locationManager = LocationManager()

  func makeUIView(context: Context) -> GMSMapView{
    let view = GMSMapView(frame: .zero)
    ...
    willAppear(context)
    return view
  }

  func updateUIView(_ mapView: GMSMapView, context: Context) { ... }

  func makeCoordinator() -> MapView.Coordinator {
    return Coordinator()
  }

  static func dismantleUIView(_ uiView: GMSMapView, coordinator: MapView.Coordinator) {
    coordinator.cancellable.removeAll()
  }

  class Coordinator {
    var cancellable = Set<AnyCancellable>()
  }
}

extension MapView {
  func willAppear(_ context: Context) {
    locationManager.didChange.sink{
      print("location: \($0)")
    }.store(in: &context.coordinator.cancellable)

    locationManager.startUpdating()
  }
}
结构映射视图:UIViewRepresentable{ @状态变量locationManager=locationManager() func makeUIView(上下文:context)->GMSMapView{ let view=GMSMapView(帧:.0) ... 将出现(上下文) 返回视图 } func updateUIView(uMapView:GMSMapView,context:context){…} func makeCoordinator()->MapView.Coordinator{ 返回协调员() } 静态函数拆卸视图(uiView:GMSMapView,coordinator:MapView.coordinator){ coordinator.cancelable.removeAll() } 班级协调员{ var cancelable=Set() } } 扩展地图视图{ func将出现(ucontext:context){ locationManager.didChange.sink{ 打印(“位置:\($0)”) }.store(在:&context.coordinator.Cancelable中) locationManager.startUpdating() } } 我认为这个方法
locationManager.startUpdating()
locationManager.StopUpdate()
需要调用另一个类