Resolved why bool value is not changed in dispatch

Discussion in 'iOS Programming' started by erdinc27, Feb 15, 2016.

  1. erdinc27 macrumors regular

    Joined:
    Jul 20, 2011
    #1
    i try to change a bool value in dispatch_async when the applicationDidEnterBackground invoked. I can read the correct value when the method is invoked but when i try to use it in dispatch_asynch then i see that it is not the right value. Here how my code looks
    Code:
    - (void)applicationDidEnterBackground:(UIApplication *)application {
       
        __block BOOL isAlarmOn = [[NSUserDefaults standardUserDefaults] boolForKey:@"alarmSituation"];
       
        __block BOOL isWarningRepeatOn = [[NSUserDefaults standardUserDefaults] boolForKey:@"warningRepeat"];
       
        NSLog(@"isWarningRepeatOn %d", isWarningRepeatOn);
       
        BOOL isVibrationOn = [[NSUserDefaults standardUserDefaults] boolForKey:@"vibration"];
       
        NSString *strSoundName = [[[NSUserDefaults standardUserDefaults] objectForKey:@"selectedSoundName"] lowercaseString];
       
        if ([[UIDevice currentDevice] isMultitaskingSupported]) {
           
            __block UIBackgroundTaskIdentifier background_task;
            background_task = [application beginBackgroundTaskWithExpirationHandler: ^ {
               
                NSLog(@"ExpirationHandler about to expire...");
            }];
           
           
            dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
               
                [self.operationQueue addOperationWithBlock:^{
                   
                    while(TRUE) {
                       
                        UIDevice *device  = [UIDevice currentDevice];
                        [device setBatteryMonitoringEnabled:YES];
                       
                        float batteryLevel = (float)[device batteryLevel] * 100;
                        self.theLevel = (int) ceil(batteryLevel);
                       
                        NSInteger desiredBatteryLevel = [[NSUserDefaults standardUserDefaults] integerForKey:@"batteryLevel"];
                       
                        if (isAlarmOn) {
                           
                            if (isWarningRepeatOn) {
                               
                                NSLog(@"ISWARNINGON %d", isWarningRepeatOn);
                            }
                           
                        }
                       
                        [[NSNotificationCenter defaultCenter] postNotificationName:@"UIDeviceBatteryLevelDidChangeNotification" object:self userInfo:nil];
                       
                        [NSThread sleepForTimeInterval:10]; //wait for 1 sec
                    }
                }];
            });
        }
    }
    Here NSLog(@"isWarningRepeatOn %d", isWarningRepeatOn) writes the right value but NSLog(@"ISWARNINGON %d", isWarningRepeatOn) writes the wrong one. What can be reason here ?
     
  2. AxoNeuron macrumors 65816

    AxoNeuron

    Joined:
    Apr 22, 2012
    Location:
    The Left Coast
    #2
    Dispatch queues are not called immediately when they are scheduled to be enqueued. They get called whenever the system thinks it is ready. I might suggest declaring these Boolean variables inside of the dispatch block. In your current code it doesn't seem like you are using them outside of this dispatch block.

    Also, you should try putting log statements in a lot more places so you can identify precisely where this value is getting changed.
     
  3. erdinc27, Feb 16, 2016
    Last edited: Feb 16, 2016

    erdinc27 thread starter macrumors regular

    Joined:
    Jul 20, 2011
    #3
    Taking boolean variables in while loop solved my problem. Thanks for your help
     
  4. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #4
    Problem resolved? If so, please see the sticky about prefixing your thread Resolved.
     

Share This Page