-1

So, first stack overflow question but a long time reader ;)

My LocationManager settings are changing back to default when it reaches didUpdateLocations. I've set up an observer in viewDidLoad as well as some defaults like so. I'm also alloc ,initing, and setting the delegate of the locationManager before anything else.

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(settingsDidChange:)
                                             name:NSUserDefaultsDidChangeNotification
                                           object:nil];

NSMutableDictionary *defaultsDictionary = [[NSMutableDictionary alloc] init];

[defaultsDictionary setObject:@(kCLLocationAccuracyNearestTenMeters) forKey:[SettingKeys LocationTrackingAccuracyPrefs]];
[[NSUserDefaults standardUserDefaults] registerDefaults:defaultsDictionary];

Here is the settingsDidChange that is being observed and where I am attempting to set the desiredAccuracy of locationManager.

- (void)settingsDidChange:(NSNotification *)notification
{
NSUserDefaults *settings = [NSUserDefaults standardUserDefaults];
CLLocationAccuracy desiredAccuracy = [settings doubleForKey:[SettingKeys LocationTrackingAccuracyPrefs]];
locationManager.desiredAccuracy = desiredAccuracy;
}

In a separate Settings view I am changing the value of desiredAccuracy which seems to be carrying over until it gets to didUpdateLocations. Then no matter what I set it to it seems to revert(or never change) to "Best" as the desiredAccuracy. I'm not sure what I'm missing here...I hope y'all can steer me in the right direction. Thanks!

rclee
  • 9
  • 3
  • Try [[NSUserDefaults standardUserDefaults] synchronize]; after registerDefaults – Frane Poljak Nov 04 '15 at 21:55
  • 1
    Have you set a breakpoint in settingsDidChange to make sure it gets called? have you checked the value of desiredAccuracy you are getting? Have you made sure that locationManager isn't nil when that code runs? (My bet is that locationManager is nil, so your settingsDidChange method isn't actually changing anything.) – Duncan C Nov 04 '15 at 21:57
  • @FranePoljak I tried the synchronize--it didn't seem to have any change. – rclee Nov 04 '15 at 22:02
  • @DuncanC locationManager is working properly and I'm getting updates to my map. settingsDidChange also works properly--I've attempted to find the source several times with breakpoints and NSLogs but the change is not getting into didUpdateLocations for some reason. If I NSLog(@"Location Accuracy: %f", locationManager.desiredAccuracy); in settingsDidChange for instance than each change I make applies properly but then any NSLog in didUpdateLocations still says that it is set to Best. – rclee Nov 04 '15 at 22:06

1 Answers1

1

Figured out my problem...further down in my settingsDidChange I was reallocating my LocationManager and that was causing it to revert to default settings. I feel stupid for not seeing it before! Thanks for the help anyway!

locationManager = [[CLLocationManager alloc] init];

ARRRGGG!!!

rclee
  • 9
  • 3
  • I was just about to post that you were on your own figuring this out because you weren't providing enough information and there was probably some detail you were getting wrong. Creating a new instance of the location manager was going to be one of my guesses. Please, when you ask as question, provide clear and detailed information about your setup. It's very frustrating to have to play 20 questions in order to try to help you. – Duncan C Nov 04 '15 at 23:25
  • @DuncanC I tried to provide all the information I thought relevant at the time. It was getting late and I simply overlooked that portion of code. My apologies and I'll try to be more detailed in the future. Thank you. – rclee Nov 05 '15 at 14:51
  • The important things are that (a) you found your problem and (b) you posted back to this thread with an answer, so others can learn (voted). You should accept your answer. – Duncan C Nov 05 '15 at 14:54