1

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];
}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
liming
  • 38
  • 1
  • 4

3 Answers3

1

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];
  });
}
Chris Butcher
  • 485
  • 5
  • 15
0

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

larva
  • 4,687
  • 1
  • 24
  • 44
  • I added MBProgressHUD into didReceiveLocalNotification, it has the same situation, app freeze about a few seconds and then pop up MBProgressHUD. – liming Jan 20 '14 at 07:23
  • By the way, the latitude and longitude were assigned by me. So I think it doesn't need time to prepare for getting location by GPS. – liming Jan 20 '14 at 07:25
  • You need to init map in viewDidLoad, add and show MBProgressHUD after init map. And when you calculator all item (in didReceiveLocalNotification) you hide MBProgressHUD and reload items in map. This map will load when you wait for items location – larva Jan 20 '14 at 08:46
  • ok, just talk about map. I have already init map, my point is why need to wait some seconds for switch map from my app. I also test openURL in didReceiveLocalNotification, it also has the same situation, I need to wait, why? – liming Jan 20 '14 at 10:39
0

You could try wrapping your code with this:

dispatch_async(dispatch_get_main_queue(), ^{
    // Your code here
});
Yazid
  • 970
  • 7
  • 14