Resolved why bool value is not changed in dispatch
- Get link
- X
- Other Apps
i try change bool value in dispatch_async when applicationdidenterbackground invoked. can read correct value when method invoked when try use in dispatch_asynch see not right value. here how code looks
here nslog(@"iswarningrepeaton %d", iswarningrepeaton) writes right value nslog(@"iswarningon %d", iswarningrepeaton) writes wrong one. can reason here ?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 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 1 sec } }]; }); } }
dispatch queues not called when scheduled enqueued. called whenever system thinks ready. might suggest declaring these boolean variables inside of dispatch block. in current code doesn't seem using them outside of dispatch block.
also, should try putting log statements in lot more places can identify precisely value getting changed.
Forums iPhone, iPad, and iPod Touch iOS Programming
- iPhone
- Mac OS & System Software
- iPad
- Apple Watch
- Notebooks
- iTunes
- Apple ID
- iCloud
- Desktop Computers
- Apple Music
- Professional Applications
- iPod
- iWork
- Apple TV
- iLife
- Wireless
- Get link
- X
- Other Apps
Comments
Post a Comment