Issues with iOS (Xamarin Forms) starting location data source

673
1
06-24-2021 08:00 AM
MichaelHamsa
Occasional Contributor

Hello,

We've been seeing a very sporadic issue on iOS when an app starts the location data source resulting in an unhandled exception. It's only happened a few times but we'd like to get to the bottom of it. We're not sure where it is coming from so we figured we post something here to see if anyone had any ideas.

Any input would be greatly appreciated.

Here's the exception and stack trace we get when the error occurs:

Invalid argument: location data source has been started.

ArcGISException.HandleCoreError (RuntimeCoreNet.GeneratedWrappers.CoreError error, System.Boolean throwException)
Interop.CheckError (System.IntPtr errorHandle, System.Boolean throwOnFailure, System.Runtime.InteropServices.GCHandle wrapperHandle)
CoreLocationDataSource.HandleStart (RuntimeCoreNet.GeneratedWrappers.CoreClientReference userDefinedError)
ICoreCallback_LocationDataSource_StartCallback.StartCallback ()
AsyncMethodBuilderCore+<>c.<ThrowAsync>b__7_0 (System.Object state)
NSAsyncSynchronizationContextDispatcher.Apply ()
(wrapper managed-to-native) UIKit.UIApplication.UIApplicationMain(int,string[],intptr,intptr)
UIApplication.Main (System.String[] args, System.IntPtr principal, System.IntPtr delegate)
UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName)
Application.Main (System.String[] args)

0 Kudos
1 Reply
JoeHershman
MVP Regular Contributor

I've seen a similar error (or maybe the same one).  I have never truly tracked it down, based on description appears it wants to try to start the location data source a second time.  Not sure if you are just using on board location or have external.

A couple things I have done, is when using an external GPS I make sure to set the LocationDisplay.DataSource on the main thread

//LocationProvider.LocationDataSource is a pointer to an external custom LocationDataSource
MainThread.BeginInvokeOnMainThread(() => mapView.LocationDisplay.DataSource = LocationProvider.LocationDataSource);

 Also make sure to wrap starting in a conditional, to be sure that we don't send a StartAsync after it's running

if ( !mapView.LocationDisplay.DataSource.IsStarted )
{
	mapView.LocationDisplay.IsEnabled = true;
}

 Good luck

Thanks,
-Joe
0 Kudos