不再使用CrossGeolocator插件接收后台位置更新

时间:2019-06-09 00:42:19

标签: xamarin.forms xamarin.ios

我已经在应用程序中使用CrossGeolocator plugin大约一年了。直到最近,我在iOS上的后台位置更新都没有遇到太多问题。

这是App.cs(共享应用程序)中的函数,用于启动位置更新以及在更新时调用的相应函数:

public static async void StartListening()
        {
            if (Device.RuntimePlatform != Device.iOS)
                return;

            Debug.WriteLine("THIS SHOULD ONLY HAPPEN ON IOS");

            if (CrossGeolocator.Current.IsListening)
                return;

            CrossGeolocator.Current.DesiredAccuracy = 10;

            await CrossGeolocator.Current.StartListeningAsync(TimeSpan.FromSeconds(LOCATION_PING_SECONDS), 0, true, new Plugin.Geolocator.Abstractions.ListenerSettings
            {
                AllowBackgroundUpdates = true,
                PauseLocationUpdatesAutomatically = false
            });

            // update the server with the last known position
            var position = await CrossGeolocator.Current.GetLastKnownLocationAsync();
            Settings.CurrentPosition = position;
            await dataStore.UpdateLocation(position.Latitude, position.Longitude);

            CrossGeolocator.Current.PositionChanged += PositionChanged;
            CrossGeolocator.Current.PositionError += PositionError;
        }

static async void PositionChanged(object sender, PositionEventArgs e)
        {
            //If updating the UI, ensure you invoke on main thread
            var position = e.Position;

            Settings.CurrentPosition = position;

            if (!Settings.IsLoggedIn)
                return;
            else if (Settings.IsAuthExpired)
                await loginStore.Refresh();

            Debug.WriteLine(string.Format("------ LOCATION UPDATED AT {0}", DateTime.Now));

            await dataStore.UpdateLocation(position.Latitude, position.Longitude);
        }

这是我的日志,显示更新之间的巨大差距:

------ LOCATION UPDATED AT 6/8/2019 8:24:34 PM
------ LOCATION UPDATED AT 6/8/2019 8:24:35 PM
Thread started: <Thread Pool> #12
------ LOCATION UPDATED AT 6/8/2019 8:24:37 PM
------ LOCATION UPDATED AT 6/8/2019 8:24:43 PM
------ LOCATION UPDATED AT 6/8/2019 8:24:47 PM
------ LOCATION UPDATED AT 6/8/2019 8:24:48 PM
------ LOCATION UPDATED AT 6/8/2019 8:24:50 PM
------ LOCATION UPDATED AT 6/8/2019 8:24:53 PM
Thread finished: <Thread Pool> #10
Thread finished: <Thread Pool> #4
Thread finished: <Thread Pool> #6
------ LOCATION UPDATED AT 6/8/2019 8:25:09 PM
Thread started: <Thread Pool> #13
Thread started: <Thread Pool> #14
------ LOCATION UPDATED AT 6/8/2019 8:25:13 PM
------ LOCATION UPDATED AT 6/8/2019 8:25:16 PM
Thread finished: <Thread Pool> #5
Thread finished: <Thread Pool> #3
Thread started: <Thread Pool> #15
------ LOCATION UPDATED AT 6/8/2019 8:25:35 PM
Thread started: <Thread Pool> #16
Thread finished: <Thread Pool> #13
Thread finished: <Thread Pool> #12
Thread finished: <Thread Pool> #16
Thread finished: <Thread Pool> #15
Thread started: <Thread Pool> #17
Thread finished: <Thread Pool> #14
Thread finished: <Thread Pool> #9
Thread started: <Thread Pool> #18
Thread started: <Thread Pool> #19
Thread finished: <Thread Pool> #18
Thread finished: <Thread Pool> #17
Thread started: <Thread Pool> #20
Thread started: <Thread Pool> #21
Thread finished: <Thread Pool> #21
Thread finished: <Thread Pool> #20
Thread started: <Thread Pool> #22
Thread finished: <Thread Pool> #19
Thread started: <Thread Pool> #23
Thread started: <Thread Pool> #24
Thread finished: <Thread Pool> #24
Thread finished: <Thread Pool> #22
Thread started: <Thread Pool> #25
Thread started: <Thread Pool> #26
Thread started: <Thread Pool> #27
Thread started: <Thread Pool> #28
Thread started: <Thread Pool> #29
------ LOCATION UPDATED AT 6/8/2019 8:30:09 PM
------ LOCATION UPDATED AT 6/8/2019 8:30:09 PM
Thread started: <Thread Pool> #30
------ LOCATION UPDATED AT 6/8/2019 8:30:09 PM
------ LOCATION UPDATED AT 6/8/2019 8:30:09 PM
------ LOCATION UPDATED AT 6/8/2019 8:30:09 PM

似乎位置更新在约1分钟后停止。第二批日志是当我将应用程序推送到后台时。将应用程序返回到前台并没有再次开始更新。

供参考,LOCATION_PING_SECONDS为15。

我的设置不正确吗?

我将切换到新的Xamarin.Essentials Geolocation库,但是它似乎缺少后台位置更新。

0 个答案:

没有答案