Question

I use NSNotification to send notification when switch is changed position to start updating annotations on map. My problem is that I have 8 switches and when user change position of few switches, map updates for many times. How a limit that to just one notification?

- (IBAction)saveSwitch:(id)sender
{

    [[NSNotificationCenter defaultCenter] postNotificationName:@"MyAppSettingsChanged" object:self userInfo:nil];


    NSUserDefaults *defs1 = [NSUserDefaults standardUserDefaults];
    [defs1 setBool: blackSwitch.on forKey: @"blackKey"];

    NSUserDefaults *defs2 = [NSUserDefaults standardUserDefaults];
    [defs2 setBool: greenSwitch.on forKey: @"greenKey"];

    NSUserDefaults *defs3 = [NSUserDefaults standardUserDefaults];
    [defs3 setBool: purpleSwitch.on forKey: @"purpleKey"];

    NSUserDefaults *defs4 = [NSUserDefaults standardUserDefaults];
    [defs4 setBool: orangeSwitch.on forKey: @"orangeKey"];

    NSUserDefaults *defs5 = [NSUserDefaults standardUserDefaults];
    [defs5 setBool: blueSwitch.on forKey: @"blueKey"];

    NSUserDefaults *defs6 = [NSUserDefaults standardUserDefaults];
    [defs6 setBool: redSwitch.on forKey: @"redKey"];

    NSUserDefaults *defs7 = [NSUserDefaults standardUserDefaults];
    [defs7 setBool: darkblueSwitch.on forKey: @"darkblueKey"];

    NSUserDefaults *defs8 = [NSUserDefaults standardUserDefaults];
    [defs8 setBool: yellowSwitch.on forKey: @"yellowKey"];

    [[NSUserDefaults standardUserDefaults] synchronize]; 

}

In another view

- (void)viewWillAppear:(BOOL)animated
{
     [super viewWillAppear:animated];

     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onAppSettingsChanged:) name:@"MyAppSettingsChanged" object:nil];

}

Updating annotations

- (void) onAppSettingsChanged:(NSNotification *)notification {

    NSLog(@"Settings was changed");

    //Create the thread 
    [NSThread detachNewThreadSelector:@selector(loadPList) toTarget:self withObject:nil];

} 
Was it helpful?

Solution

You must update some BOOLean value to indicate that you are not currently processing an update, and proceed accordingly. I.E.

- (void) onAppSettingsChanged:(NSNotification *)notification {
    NSLog(@"Settings was changed");
    if (!myBoolToIndicateProcessing) {
      //Create the thread
      myBoolToIndicateProcessing = YES;
      [NSThread detachNewThreadSelector:@selector(loadPList) toTarget:self withObject:nil];
    }
} 

Then, when the reload has finished, set the BOOL back to NO.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top