Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There really is no way for you to check "is my delegate 'active' for this locationmanager?".</p> <p>What you need to do is keep track yourself of whether you're done with the location updates. </p> <p>I'm not sure if this is what you need, but here's what i do:</p> <p>1- when i start requesting updates, i do as you do, i.e. through requestlocationupdates and a delegate.</p> <p>2- In the case where i don't get the updates i need, i want to time out after some time, so that my app doesn't wait for a location forever, in this case i use a Selector with a timeout:</p> <pre><code>[self performSelector:@selector(fetchLocationTimeout) withObject:nil afterDelay:LOCATION_LISTEN_TIME]; </code></pre> <p>and in the selector:</p> <pre><code>(void)fetchLocationTimeout{ if(!finished){ [self stopUpdatingLocation]; [delegate locationFinished:bestEffortAtLocation];//see more on this below } } </code></pre> <p>3- in my didupdate-delegate i both store away the "best" location i have gotten, and if i decide that the new location is "good enough", i finish my location-fetching process:</p> <pre><code>(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{ if(newLocation is the best i have){ self.bestEffortAtLocation = newLocation; } // test the measurement to see if it is more accurate than the previous measurement if (bestEffortAtLocation == nil || newLocation is the best i have) { // store the location as the "best effort" if (bestEffortAtLocation is good enough and i want to use it) { [self stopUpdatingLocation]; // we can also cancel our previous performSelector:withObject:afterDelay: - it's no longer necessary SEL selector = NSSelectorFromString(@"fetchLocationTimeout"); [NSObject cancelPreviousPerformRequestsWithTarget:self selector:selector object:nil]; [delegate locationFinished:bestEffortAtLocation]; } } } </code></pre> <p>Finally, this is my method which is called above, both from within the didupdate-delegate and from the selector timeout to reduce the risk of a race condition:</p> <pre><code>(void)stopUpdatingLocation{ finished = true; NSLog(@"stopUpdatingLocation! manager: %@", [locationManager description]); [locationManager stopUpdatingLocation]; locationManager.delegate = nil; } </code></pre> <p>Final note: the "delegate" i'm calling when i'm done is my own interface describing a class that knows how to take care of the result of a location-fetching process. it has only one method to be implemented "locationFinished", as you can see.</p> <p>Hope this helps even though my code-pasting skills aren't exactly ninja.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload