open map app slowly under didReceiveLocalNotification

334 views Asked by At

I want to do a simple action, get a local notification and open apple map app. When I get the notification and then slide it, my app open first that is fine. But I need to wait about 10 seconds and the map app will be opened. why does it delay? It should be my app open and then the map does directly.

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
  CLLocationCoordinate2D rdOfficeLocation = CLLocationCoordinate2DMake(latitude,longitude);
  MKPlacemark *placemark = [[MKPlacemark alloc] initWithCoordinate:rdOfficeLocation addressDictionary:nil];
  MKMapItem *item = [[MKMapItem alloc] initWithPlacemark:placemark]; 
  [item openInMapsWithLaunchOptions:nil];
}
3

There are 3 answers

0
Yazid On BEST ANSWER

You could try wrapping your code with this:

dispatch_async(dispatch_get_main_queue(), ^{
    // Your code here
});
4
larva On

you can add some thing like indicator or MBProgressHUD when app get location. If you want nothing display in screen you can add an view appear in map and when you loaded all hide this view with animation

0
Chris Butcher On

I've just been having this problem myself, not with maps but with using Custom URL schemes to deep-link to other apps. The problem is that your app has not finished resuming when you're telling it to switch into Maps. This causes the 10 second delay. If you debug, you will probably find that didReceiveLocalNotification is executing and hangs for a while on your call to Maps. Then after that call completes, it still has to execute applicationDidBecomeActive inside your AppDelegate.

As @Yazid suggests, wrap it with an async call, so that your app can fire off it's Maps integration in a separate thread, whilst resuming itself quickly on the main thread. As soon as that completes (which will be much much faster) you will see Maps open. However I'd advise using the default queue rather than the main queue (you could get a deadlock using the main queue).

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
{
  dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    CLLocationCoordinate2D rdOfficeLocation = CLLocationCoordinate2DMake(latitude,longitude);
    MKPlacemark *placemark = [[MKPlacemark alloc] initWithCoordinate:rdOfficeLocation addressDictionary:nil];
    MKMapItem *item = [[MKMapItem alloc] initWithPlacemark:placemark]; 
    [item openInMapsWithLaunchOptions:nil];
  });
}