我将JSON数据放入表格视图中,并且我尝试使用for循环解析数据。但是,当循环完成解析JSON数据并将20个项放入表视图时,它会重新启动进程,再次解析JSON,同样的数据再次出现在表视图中。这个过程也会重复很长时间。
`
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
guard let location = locations.last else{ return }
searchURL = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=\(location.coordinate.latitude),\(location.coordinate.longitude)&radius=50000&types=night_club&key=MY_API_KEY"
cityInfo = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=\(location.coordinate.latitude),\(location.coordinate.longitude)&radius=50000&types=locality&key=MY_API_KEY"
locationManager.stopUpdatingLocation()
locationManager.delegate = nil
getCityInfo(url: cityInfo)
callAlamo(url: searchURL)
}
更新
正如vadian在他的第一条评论中提到的那样,parseData()被多次调用。所以我添加了
locationManager.delegate = nil
在我停止更新locationManager委托函数中的位置后。
==14227== HEAP SUMMARY:
==14227== in use at exit: 72,704 bytes in 1 blocks
==14227== total heap usage: 3 allocs, 2 frees, 73,760 bytes allocated
==14227==
==14227== LEAK SUMMARY:
==14227== definitely lost: 0 bytes in 0 blocks
==14227== indirectly lost: 0 bytes in 0 blocks
==14227== possibly lost: 0 bytes in 0 blocks
==14227== still reachable: 72,704 bytes in 1 blocks
==14227== suppressed: 0 bytes in 0 blocks
==14227== Rerun with --leak-check=full to see details of leaked memory
==14227==
`
此后其他所有内容都保持不变。
答案 0 :(得分:0)
我怀疑您多次致电parseData
。解决方案是在委托方法中停止监视位置。
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
guard let location = locations.last else { return }
let searchURL = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=\(location.coordinate.latitude),\(location.coordinate.longitude)&radius=50000&types=night_club&key=AIzaSyA2LQsGK_I1ETnKPGbjWgFW9onZlHog6dg"
// var cityInfo = NSString(format: "https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=%f,%f&radius=50000&types=locality&key=AIzaSyA2LQsGK_I1ETnKPGbjWgFW9onZlHog6dg", (location?.coordinate.latitude)!,(location?.coordinate.longitude)!) as? String
manager.stopUpdatingLocation()
callAlamo(url: searchURL)
}
我编辑了方法的主体,以避免所有问题和感叹号。
附注:基本上不注释编译器可以推断的类型。