<commit_before>using System;
using MonoTouch.CoreLocation;
using System.Threading.Tasks;
using System.Threading;

namespace Xamarin.Geolocation
{
	internal class GeolocationSingleUpdateDelegate
		: CLLocationManagerDelegate
	{
		public GeolocationSingleUpdateDelegate (CLLocationManager manager, double desiredAccuracy, int timeout, CancellationToken cancelToken)
		{
			this.manager = manager;
			this.tcs = new TaskCompletionSource<Position> (manager);
			this.desiredAccuracy = desiredAccuracy;
			
			if (timeout != Timeout.Infinite)
			{
				Timer t = null;
				t = new Timer (s =>
				{
					if (this.haveLocation)
						this.tcs.TrySetResult (new Position (this.position));
					else
						this.tcs.TrySetCanceled();
						
					StopListening();
					t.Dispose();
				}, null, timeout, 0);
			}
			
			cancelToken.Register (() =>
			{
				this.tcs.TrySetCanceled();
				StopListening();
			});
		}
		
		public Task<Position> Task
		{
			get { return this.tcs.Task; }
		}

		public override void AuthorizationChanged (CLLocationManager manager, CLAuthorizationStatus status)
		{
			// If user has services disabled, we're just going to throw an exception for consistency.
			if (status == CLAuthorizationStatus.Denied || status == CLAuthorizationStatus.Restricted)
			{
				this.tcs.TrySetException (new GeolocationException (GeolocationError.Unauthorized));
				StopListening();
			}
		}

		public override void Failed (CLLocationManager manager, MonoTouch.Foundation.NSError error)
		{
			switch ((CLError)error.Code)
			{
				case CLError.Network:
					this.tcs.SetException (new GeolocationException (GeolocationError.PositionUnavailable));
					StopListening();
					break;
			}
		}<commit_msg>[iOS] Stop listening before firing events<commit_after>using System;
using MonoTouch.CoreLocation;
using System.Threading.Tasks;
using System.Threading;

namespace Xamarin.Geolocation
{
	internal class GeolocationSingleUpdateDelegate
		: CLLocationManagerDelegate
	{
		public GeolocationSingleUpdateDelegate (CLLocationManager manager, double desiredAccuracy, int timeout, CancellationToken cancelToken)
		{
			this.manager = manager;
			this.tcs = new TaskCompletionSource<Position> (manager);
			this.desiredAccuracy = desiredAccuracy;
			
			if (timeout != Timeout.Infinite)
			{
				Timer t = null;
				t = new Timer (s =>
				{
					if (this.haveLocation)
						this.tcs.TrySetResult (new Position (this.position));
					else
						this.tcs.TrySetCanceled();
						
					StopListening();
					t.Dispose();
				}, null, timeout, 0);
			}
			
			cancelToken.Register (() =>
			{
				StopListening();
				this.tcs.TrySetCanceled();
			});
		}
		
		public Task<Position> Task
		{
			get { return this.tcs.Task; }
		}

		public override void AuthorizationChanged (CLLocationManager manager, CLAuthorizationStatus status)
		{
			// If user has services disabled, we're just going to throw an exception for consistency.
			if (status == CLAuthorizationStatus.Denied || status == CLAuthorizationStatus.Restricted)
			{
				StopListening();
				this.tcs.TrySetException (new GeolocationException (GeolocationError.Unauthorized));
			}
		}

		public override void Failed (CLLocationManager manager, MonoTouch.Foundation.NSError error)
		{
			switch ((CLError)error.Code)
			{
				case CLError.Network:
					StopListening();	
					this.tcs.SetException (new GeolocationException (GeolocationError.PositionUnavailable));
					break;
			}
		}
{'fraction_non_alphanumeric': 0.09712125617912183, 'fraction_numerical': 0.0005815644082582146, 'mean_word_length': 3.629878869448183, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4155331', 'n_tokens_mistral': 1276, 'n_tokens_neox': 1024, 'n_words': 233}