MKDirections calculateETAWithCompletionHandler: in background state

I have an application that tracks significant changes in location.
After receiving a new calculation, I want to calculate the duration from the current location to the specified location.
To calculate the duration, I am using calculateETAWithCompletionHandler:

from class MKDirections

.
Everything works as expected while the application is in the foreground.

When I send the app to the background, it correctly receives location updates in the background and everything works until I call calculateETAWithCompletionHandler:

, which will never return results.

MKDirectionsHandler

, completion handler calculateETAWithCompletionHandler:

. never called when in the background.

As soon as the application returns to the foreground, all wait completion handlers will receive the results.

MKMapItem* origin = [MKMapItem mapItemForCurrentLocation];
MKMapItem* destination = [[MKMapItem alloc] initWithPlacemark:destinationPlacemark];

MKDirectionsRequest* request = [MKDirectionsRequest new];
[request setSource:origin];
[request setDestination:destination];
[request setTransportType:MKDirectionsTransportTypeAutomobile];

MKDirections* directions = [[MKDirections alloc] initWithRequest:request];

[directions calculateETAWithCompletionHandler:^(MKETAResponse *response, NSError *error) {
        completion(response.expectedTravelTime, error);
}];

      

Is a call calculateETAWithCompletionHandler:

in the background invalid?
Is there a way to solve this problem?

+3


source to share


2 answers


I believe the issue with MKMapItem is a problem, you need to run it on the main thread. So I don't think this will work for what you need. When collecting locations in the background, you should use CoreLocation instead.



+3


source


The documentation is MKDirection

n't very specific about this issue, the most relevant section I could find was:

The MKDirections object provides you with route data from Apple servers. You can use instances of this class to retrieve travel or driving or walking time information based on the data in the MKDirectionsRequest object you provide. The referral object passes your request to Apple's servers and returns the requested information to the block you provide.



Since you are trying to calculate travel times, it turns out that it is calculateETAWithCompletionHandler:

trying to fulfill a network request to apple's servers. If the app is in the background, the request is suspended until the app is brought to the fore.

Unfortunately, I don't think there is an easy way around this. You can try and use the "guesstimation" approach where before the app enters the background it calculates the ETA for the user and then when it is in the background it increases or decreases the ETA in proportion to the direct distance between your current location and destination. Depending on how accurate you want your results to be that broad, it may be enough to meet your requirements.

0


source







All Articles