我试图测试Apple正向地理编码服务的准确性,以涵盖我们的数据模型上的纬度/经度缺失或无效的罕见情况,但我们仍然知道物理地址。 Monotouch提供CLGeocoder.GeocodeAddress(String, CLGeocodeCompletionHandler)
和GeocodeAddressAsync(String)
,但是当我调用它们时,从不调用completionHandler,异步方法永远不会返回。
我的应用程序日志中没有任何内容表明存在问题,并且将调用封装在try-catch块中并没有发现任何异常。在项目选项中启用了地图集成。没有捕获网络流量(无论如何可能是SSL)我没有想法。
这是加载地标并尝试对地址进行地理编码的代码:
private void ReloadPlacemarks()
{
// list to hold any placemarks which come back with empty/invalid coordinates
List<ServiceCallWrapper> geoList = new List<ServiceCallWrapper> ();
mapView.ClearPlacemarks ();
List<MKPlacemark> placemarks = new List<MKPlacemark>();
if (serviceCallViewModel.ActiveServiceCall != null) {
var serviceCall = serviceCallViewModel.ActiveServiceCall;
if (serviceCall.dblLatitude != 0 && serviceCall.dblLongitude != 0) {
placemarks.Add (serviceCall.ToPlacemark ());
} else {
// add it to the geocode list
geoList.Add (serviceCall);
}
}
foreach (var serviceCall in serviceCallViewModel.ServiceCalls) {
if (serviceCall.dblLatitude != 0 && serviceCall.dblLongitude != 0) {
placemarks.Add (serviceCall.ToPlacemark ());
} else {
//add it to the geocode list
geoList.Add (serviceCall);
}
}
if (placemarks.Count > 0) {
mapView.AddPlacemarks (placemarks.ToArray ());
}
if (geoList.Count > 0) {
// attempt to forward-geocode the street address
foreach (ServiceCallWrapper s in geoList) {
ServiceCallWrapper serviceCall = GeocodeServiceCallAddressAsync (s).Result;
mapView.AddPlacemark (serviceCall.ToPlacemark());
}
}
}
private async Task<ServiceCallWrapper> GeocodeServiceCallAddressAsync(ServiceCallWrapper s)
{
CLGeocoder geo = new CLGeocoder ();
String addr = s.address + " " + s.city + " " + s.state + " " + s.zip;
Console.WriteLine ("Attempting forward geocode for service call UID: " + s.call_uid + " with address: " + addr);
//app hangs on this
CLPlacemark[] result = await geo.GeocodeAddressAsync(addr);
//code updating latitude and longitude (omitted)
return s;
}
答案 0 :(得分:5)
你的问题就在这一行:
ServiceCallWrapper serviceCall = GeocodeServiceCallAddressAsync (s).Result;
通过调用Task<T>.Result
,您将导致死锁。我完全解释了这一点on my blog,但它的要点是await
将(默认情况下)捕获&#34; context&#34;当它产生控制时,将使用该上下文来完成async
方法。在这种情况下,&#34; context&#34;是UI上下文。因此,当响应进入时,UI线程被阻塞(等待Result
),并且async
方法无法继续,因为它等待在UI线程上运行。
解决方案是一直使用async
。换句话说,将每个Task<T>.Result
和Task.Wait
替换为await
:
private async Task ReloadPlacemarksAsync()
{
...
ServiceCallWrapper serviceCall = await GeocodeServiceCallAddressAsync (s);
...
}
请注意,您的void ReloadPlacemarks
现在为Task ReloadPlacemarksAsync
,因此此更改会影响您的来电者。 async
将{&#34;}&#34;成长&#34;通过代码库,这是正常的。有关详细信息,请参阅我的MSDN article on async best practices。