Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

erdinc27

macrumors regular
Original poster
Jul 20, 2011
168
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 ?
 

AxoNeuron

macrumors 65816
Apr 22, 2012
1,251
855
The Left Coast
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.
 

erdinc27

macrumors regular
Original poster
Jul 20, 2011
168
1
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.

Taking boolean variables in while loop solved my problem. Thanks for your help
 
Last edited:
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.