如何调试 ObservedObject 不再接收 Published 变量更改的原因?
How can I debug why an ObservedObject is no longer receiving a Published variable change?
在我的应用程序中实施新功能后,我收到报告说我的搜索栏不再正常运行。进一步调试后,我将其缩小到 ObservedObject
没有收到来自我的 Published
变量的更改。
LocationHandler
class 中的调试语句打印出来,我可以在不同的位置放置断点以获得 @Published
变量打印输出但是,当我放置断点时我使用 ObservedObject
、locationHandler.lhLatitude
的 UIViewRepresentable
正在打印 0.0
,我不确定为什么会这样。我很乐意继续对此进行进一步调试,但是我不确定如何继续进行此类调试。
class LocationHandler: ObservableObject {
@Published var lhLatitude: Double
@Published var lhLongitude: Double
internal init(lhLatitude: Double, lhLongitude: Double) {
self.lhLatitude = lhLatitude
self.lhLongitude = lhLatitude
}
func changeRegion(searchArea: String) {
let searchRequest = MKLocalSearch.Request()
searchRequest.naturalLanguageQuery = searchArea
let search = MKLocalSearch(request: searchRequest)
search.start { (response, error) in
guard let response = response else {
// MARK: Consider an alert here or some sort of thing notice rather then having no resposne?
return
}
let freshLocation = response.mapItems.first?.placemark.coordinate
self.lhLatitude = freshLocation!.latitude
self.lhLongitude = freshLocation!.longitude
// MARK: Debugging.
if (freshLocation!.latitude > 0.0) {
print("Fresh Latitude: \(self.lhLatitude)")
print("Fresh Longitude: \(self.lhLongitude)")
}
}
} // End of changeRegion function.
}
// This is on a UIViewRepresentable.
@ObservedObject var locationHandler: LocationHandler
// This is located in the updateView function and this is it's use-case, I've yet to have an issue with it until this immediate instance.
if locationHandler.lhLongitude > (0.0) {
print("Location Handler Latitude : \(locationHandler.lhLatitude)")
view.setRegion(MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: locationHandler.lhLatitude, longitude: locationHandler.lhLongitude), latitudinalMeters: defaultSpan, longitudinalMeters: defaultSpan), animated: true)
// MARK: Debug
print(#function)
}
原因被确定为不当使用传递 MKCoordinateRegion
的状态。每次用户提交搜索时,它都会触发但会重新加载子视图,然后更改状态。
在我的应用程序中实施新功能后,我收到报告说我的搜索栏不再正常运行。进一步调试后,我将其缩小到 ObservedObject
没有收到来自我的 Published
变量的更改。
LocationHandler
class 中的调试语句打印出来,我可以在不同的位置放置断点以获得 @Published
变量打印输出但是,当我放置断点时我使用 ObservedObject
、locationHandler.lhLatitude
的 UIViewRepresentable
正在打印 0.0
,我不确定为什么会这样。我很乐意继续对此进行进一步调试,但是我不确定如何继续进行此类调试。
class LocationHandler: ObservableObject {
@Published var lhLatitude: Double
@Published var lhLongitude: Double
internal init(lhLatitude: Double, lhLongitude: Double) {
self.lhLatitude = lhLatitude
self.lhLongitude = lhLatitude
}
func changeRegion(searchArea: String) {
let searchRequest = MKLocalSearch.Request()
searchRequest.naturalLanguageQuery = searchArea
let search = MKLocalSearch(request: searchRequest)
search.start { (response, error) in
guard let response = response else {
// MARK: Consider an alert here or some sort of thing notice rather then having no resposne?
return
}
let freshLocation = response.mapItems.first?.placemark.coordinate
self.lhLatitude = freshLocation!.latitude
self.lhLongitude = freshLocation!.longitude
// MARK: Debugging.
if (freshLocation!.latitude > 0.0) {
print("Fresh Latitude: \(self.lhLatitude)")
print("Fresh Longitude: \(self.lhLongitude)")
}
}
} // End of changeRegion function.
}
// This is on a UIViewRepresentable.
@ObservedObject var locationHandler: LocationHandler
// This is located in the updateView function and this is it's use-case, I've yet to have an issue with it until this immediate instance.
if locationHandler.lhLongitude > (0.0) {
print("Location Handler Latitude : \(locationHandler.lhLatitude)")
view.setRegion(MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: locationHandler.lhLatitude, longitude: locationHandler.lhLongitude), latitudinalMeters: defaultSpan, longitudinalMeters: defaultSpan), animated: true)
// MARK: Debug
print(#function)
}
原因被确定为不当使用传递 MKCoordinateRegion
的状态。每次用户提交搜索时,它都会触发但会重新加载子视图,然后更改状态。