FusedLocationProviderClient RemoveLocationUpdatesAsync 任务未完成
FusedLocationProviderClient RemoveLocationUpdatesAsync Task does not complete
我正在实施 Xamarin Android 位置跟踪器并添加了以下代码以停止位置更新。
public Task StopAsync()
{
if(IsTracking)
{
IsTracking = false;
perviousStopTask = fusedLocationProviderClient.RemoveLocationUpdatesAsync(this);
}
return perviousStopTask;
}
以上方法,我将 RemoveLocationUpdatesAsync
方法中的任务 return 分配给 perviousStopTask
和 StopAsync
方法中的 return。但是当我尝试等待这个 StopAsync
方法时,它没有完成。
我不确定 Fused Location Provider 是否线程安全。但是,您可以尝试以下一些操作。
当 IsTracking
为 false
时,您似乎返回的是已完成或 null
,因此我将方法更改为:
public Task StopAsync()
{
if (IsTracking)
{
IsTracking = false;
return fusedLocationProviderClient.RemoveLocationUpdatesAsync(this);
}
return Task.CompletedTask;
}
如果您真的不想返回调用该方法的上下文,请确保调用 .ConfigureAwait(false)
:
await StopAsync().ConfigureAwait(false);
Xamarin LocationSample project 将此问题归咎于 Google Play 服务。该示例当前在调用 RemoveLocationUpdatesAsync
.
时未使用 await
TODO: We should await this call but there appears to be a bug in Google Play Services where the first time removeLocationUpdates is called, the returned Android.Gms.Tasks.Task never actually completes, even though location updates do seem to be removed and stop happening. For now we'll just fire and forget as a workaround.
我正在实施 Xamarin Android 位置跟踪器并添加了以下代码以停止位置更新。
public Task StopAsync()
{
if(IsTracking)
{
IsTracking = false;
perviousStopTask = fusedLocationProviderClient.RemoveLocationUpdatesAsync(this);
}
return perviousStopTask;
}
以上方法,我将 RemoveLocationUpdatesAsync
方法中的任务 return 分配给 perviousStopTask
和 StopAsync
方法中的 return。但是当我尝试等待这个 StopAsync
方法时,它没有完成。
我不确定 Fused Location Provider 是否线程安全。但是,您可以尝试以下一些操作。
当
IsTracking
为false
时,您似乎返回的是已完成或null
,因此我将方法更改为:public Task StopAsync() { if (IsTracking) { IsTracking = false; return fusedLocationProviderClient.RemoveLocationUpdatesAsync(this); } return Task.CompletedTask; }
如果您真的不想返回调用该方法的上下文,请确保调用
.ConfigureAwait(false)
:await StopAsync().ConfigureAwait(false);
Xamarin LocationSample project 将此问题归咎于 Google Play 服务。该示例当前在调用 RemoveLocationUpdatesAsync
.
TODO: We should await this call but there appears to be a bug in Google Play Services where the first time removeLocationUpdates is called, the returned Android.Gms.Tasks.Task never actually completes, even though location updates do seem to be removed and stop happening. For now we'll just fire and forget as a workaround.