How I can fix the warning about locationManagerDidChangeAuthorization and authorizationStatus in #Xamarin.Forms?

huangapple go评论122阅读模式
英文:

How I can fix the warning about locationManagerDidChangeAuthorization and authorizationStatus in #Xamarin.Forms?

问题

首先,我做了一些研究,但只找到了适用于Swift的iOS本机答案。

完整的警告消息如下:

此方法如果在主线程上调用可能会导致UI无响应。相反,考虑等待-locationManagerDidChangeAuthorization:回调并首先检查authorizationStatus

我已经阅读了这可能是因为我在使用异步调用。我也阅读了有关iOS版本的信息:StackOverFlow/73805219
我应该如何在Xamarin Forms中修复这个问题?

我必须说我正在使用#Xamarin.Forms.Essentials中的Geolocalization来在异步函数中获取当前的经度和纬度,然后通过#MessaginCenter发送它。

  1. async Task StoringNoteAsync()
  2. {
  3. Location location = await _geolocation.GetCurrentLocation();
  4. NoteSelected = NoteSelected ?? new Note();
  5. NoteSelected.Title = Title;
  6. NoteSelected.Content = Content;
  7. NoteSelected.CreatedAt = DateTime.Now;
  8. NoteSelected.iNoteType = (int)SelectedNoteType;
  9. NoteSelected.Longitude = location.Longitude;
  10. NoteSelected.Latitude = location.Latitude;
  11. //_noteService.SaveNote( NoteSelected );
  12. MessagingCenter.Instance.Send( this, "upsert", NoteSelected );
  13. await _navigation.PopAsync();
  14. }
英文:

First of all, I did a little research but only found for Swift a native iOS answer.

The complete warning says:

  1. This method can cause UI unresponsiveness if invoked on the main thread. Instead, consider waiting for the `-locationManagerDidChangeAuthorization:` callback and checking `authorizationStatus` first.

I have read that could be because I am using an async, away call; as well. I have read about the iOS version also: StackOverFlow/73805219
How I can fix this in Xamarin forms?

I must say that I am using Geolocalization from #Xamarin.Forms.Essentials to get the current longitud and latitude in an async function that I send by #MessaginCenter.

  1. async Task StoringNoteAsync()
  2. {
  3. Location location = await _geolocation.GetCurrentLocation();
  4. NoteSelected = NoteSelected ?? new Note();
  5. NoteSelected.Title = Title;
  6. NoteSelected.Content = Content;
  7. NoteSelected.CreatedAt = DateTime.Now;
  8. NoteSelected.iNoteType = (int)SelectedNoteType;
  9. NoteSelected.Longitude = location.Longitude;
  10. NoteSelected.Latitude = location.Latitude;
  11. //_noteService.SaveNote( NoteSelected );
  12. MessagingCenter.Instance.Send( this, "upsert", NoteSelected );
  13. await _navigation.PopAsync();
  14. }

答案1

得分: 0

以下是翻译好的内容:

听起来你正在UI线程上运行代码,需要在不同的线程上运行代码。

使用 Task.Run 切换到后台线程。

由于你在一个 async 方法中,并且已经在主线程上运行,可以这样做:

  1. async Task MyMethod()
  2. {
  3. // "await",以便在此代码完成之前不运行下面的 "additional code"。
  4. // "async" 只在块中的代码使用 "await" 时需要。
  5. await Task.Run( async () =>
  6. {
  7. // 需要在后台线程上运行的代码...
  8. });
  9. // 需要在原始线程上运行的附加代码...
  10. ...
  11. }

在主线程上显式运行附加代码的替代方法

  1. async Task MyMethod()
  2. {
  3. // (这部分与第一个代码片段相同。)
  4. // "await",以便在此代码完成之前不运行下面的 "additional code"。
  5. // "async" 只在块中的代码使用 "await" 时需要。
  6. await Task.Run( async () =>
  7. {
  8. // 需要在后台线程上运行的代码...
  9. // 例如,不涉及UI的长时间运行逻辑。
  10. });
  11. // (这部分显式在主线程上运行。)
  12. // "async" 只在块中的代码使用 "await" 时需要。
  13. await MainThread.InvokeOnMainThreadAsync( async () =>
  14. {
  15. // 需要在主线程上运行的代码(影响UI)...
  16. });
  17. }
英文:

Sounds like you are on the UI thread, and need to run code on a different thread.

Use Task.Run to get to a background thread.

Since you are in an async method, and you are already running on MainThread, do it like this:

  1. async Task MyMethod()
  2. {
  3. // "await", so that "additional code" below is not run until this code finishes.
  4. // "async" is only needed if code in block uses "await".
  5. await Task.Run( async () =>
  6. {
  7. // code that needs to run on a background thread...
  8. });
  9. // additional code, that needs to run on the original thread...
  10. ...
  11. }

ALTERNATIVE TO EXPLICITLY RUN ADDITIONAL CODE ON MAIN THREAD

  1. async Task MyMethod()
  2. {
  3. // (This part is same as first code snippet.)
  4. // "await", so that "additional code" below is not run until this code finishes.
  5. // "async" is only needed if code in block uses "await".
  6. await Task.Run( async () =>
  7. {
  8. // code that needs to run on a background thread...
  9. // e.g. long-running logic that does not touch UI.
  10. });
  11. // (This part is explicitly run on MainThread.)
  12. // "async" is only needed if code in block uses "await".
  13. await MainThread.InvokeOnMainThreadAsync( async () =>
  14. {
  15. // code that needs to run on MainThread (affects UI)...
  16. });
  17. }

huangapple
  • 本文由 发表于 2023年6月8日 22:44:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/76433036.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定