Alexander
Alexander

Reputation: 1288

Why doesn't CLLocationManager deliver events to handle?

If I create CLLocationManager instance only on UIThread, LocationUpdated event will fired. Why does this happen? There is no any clue in Xamarin and Apple documentation that CLLocationManager must be created on UIThread.

Some code asks locationManager.RequestWhenInUseAuthorization (); NSLocationWhenInUseUsageDescription is setted in Info.plist

private void CreateLocationManagerWorkingOption () {
    ExecuteOnMainThread (() => {
        locationManager = new CLLocationManager ();
    });
    locationManager.LocationsUpdated += (object sender, CLLocationsUpdatedEventArgs e) => {
        OnLocationChanged (locationManager,e.Locations [e.Locations.Length - 1]);
    };
}

private void CreateLocationManagerNotWorkingOption () {
    ExecuteOnSomeThread(()=> {
        locationManager = new CLLocationManager ();
    });
    locationManager.LocationsUpdated += (object sender, CLLocationsUpdatedEventArgs e) => {
        OnLocationChanged (locationManager,e.Locations [e.Locations.Length - 1]);
    };
}

private void StartTrackingImpl() {
    ExecuteOnMainThread (() => locationManager?.StartUpdatingLocation ());
}

Upvotes: 2

Views: 507

Answers (2)

Evgeny Valavin
Evgeny Valavin

Reputation: 56

I guess I know why. First of all, I was experiencing the same problem. I could not get LocationUpdates working on three different devices. I've tested a lot, but CLLocationManager event still was not fired. I came across this question and finally figured out why events were not fired on my devices. I instantiated CLLoationManager in a threadPool. ThreadPools are managed by .NET. So, a thread, where I instantiated CLLoationManager were finished after a while, therefore there's nowhere to fire the events. Hope my answer helps!

Upvotes: 2

shallowThought
shallowThought

Reputation: 19600

You can create and handle it from every thread that has an active run loop.

From the CLLocationManagerDelegate documentation:

The methods of your delegate object are called from the thread in which you started the corresponding location services. That thread must itself have an active run loop, like the one found in your application’s main thread.

Upvotes: 0

Related Questions