0

First of all i am using parse.com to store information. This code simply opens the Maps app every time this method is run and saves the users location in a server.

   MKDirectionsRequest *request = [[MKDirectionsRequest alloc] init];
            [request setSource:[MKMapItem mapItemForCurrentLocation]];
            [request setDestination:endingItem];
            [request setTransportType:MKDirectionsTransportTypeAutomobile];
            [request setRequestsAlternateRoutes:YES];
            MKDirections *directions = [[MKDirections alloc] initWithRequest:request];
            [directions calculateDirectionsWithCompletionHandler:^(MKDirectionsResponse *response, NSError *error) {
                if ( ! error && [response routes] > 0) {
                    MKRoute *route = [[response routes] objectAtIndex:0];
                    //route.distance  = The distance
                    NSLog(@"total %f",route.expectedTravelTime );
                    int time = ceil(route.expectedTravelTime/60);
                    self.ETA = [@(time) stringValue];
                    NSLog(@"test %d",time);
                    NSLog(@"Total Distance (in Meters) :%0.1f",route.distance/1000);
                    self.distance = [@(route.distance*4899) stringValue];

                    // IF decline was pressed, need to fix if it's accepted
                    NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
                    [params setObject:self.distance forKey:@"dist"];

                    [PFCloud callFunctionInBackground:@"sendAccepted" withParameters:params block:^(id object, NSError *error) {
                        if (!error) {
                            NSLog(@"Success answer sent");
                        } else {
                            NSLog(@"Failed to push");
                        }

                    }];


                }
            }];


            [endingItem openInMapsWithLaunchOptions:launchOptions];

        }

What i noticed is that if Maps application is already open when this method is run then it does not save the users data until i return to the applikation. HOWEVER if i close the Maps application before this method is run the it is always sent to the server.

Now the problem i think is that it obviously takes more time for Maps app to open if it was not opened before hence giving my applikation more time to complete the update. How can i solve this so it will still update the location even if my applikation goes to the background?

Timo Cengiz
  • 3,367
  • 4
  • 23
  • 45
  • If I understood correctly your problem, I can recommend you to read this guide :https://developer.apple.com/library/ios/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html – protikhonoff Feb 05 '15 at 20:16
  • That could be the solution but how would that work? SInce i am executing the call to the server in that method above. When Maps is opened the application moves to applicationDidEnterBackground i cant execute the call to the server again? You get what i am saying? It would be a duplicate call then. @protikhonoff – Timo Cengiz Feb 05 '15 at 21:28
  • You can to wrap this using NSOperation and than ask to NSOperationQueue for waiting. Something like this: http://stackoverflow.com/questions/11376873/background-task-with-nsoperation-and-nsoperationqueue-ios – protikhonoff Feb 05 '15 at 22:22
  • Actually all i did was put the code that opens Maps app after the server call. Lol why did i not think of that earlier... @protikhonoff – Timo Cengiz Feb 06 '15 at 14:54

0 Answers0