0

I am new to iOS development and I am writing a 3-View Location Application. The first view is the main view of the application, the second view is a table view with several locations and the third view is a detail view, where the user is able to edit or add new Locations to the table view.

I'm using the CLLocationManager in the first and in the third view but every view of both has got his own CLLocationManager instances, because for the detail view I need the best accuracy whereas in the MainView I dont need the best accuracy.

So here is the problem: In my AppDelegate.m I have got a notification, which fires when the Applications enters foreground:

- (void)applicationWillEnterForeground:(UIApplication *)application
{

    [[NSNotificationCenter defaultCenter] postNotificationName: @"didEnterForeground" object: nil  userInfo: nil];
}

In my third view, the DetailViewController.m, I register in the viewDidLoad for this notification:

- (void) viewDidLoad {

    [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(enteredBackground:) name: @"didEnterBackground"  object: nil];
    [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(enteredForeground:) name: @"didEnterForeground"  object: nil];
}

The enteredForeground Method in DetailViewController.m just needs to start the Location Manager again (the didEnterBackground Method stopped him)

- (void) enteredForeground: (NSNotification*) notification {
    [self.locationManager startUpdatingLocation];
}

I am using XCode 4.2 with ARC. The problem is, that if I visited the DetailView for about 10 times, go to background (f.e. from the MainView), then I enter foreground again then 10 LocationManagers will be started immediately (this is what my NSLog says). It seems that for the DetailView (and for the other Views) the same number of instances like the number of visits for these views exist.

Maybe the views dont get released properly if they disappeared, perhaps because of the NSNotification?!

I would appreciate if someone could help me with this matter, cause so many LocationManagers will stress the battery pretty hard.

Thanks in advance!

Philipp Schlösser
  • 5,179
  • 2
  • 38
  • 52
android
  • 247
  • 1
  • 3
  • 12

1 Answers1

0

I believe you need to stopUpdatingLocation when it goes to the background. Otherwise, it will spawn multiple instances.

James
  • 2,346
  • 1
  • 16
  • 18