I’m starting a background task in -applicationDidEnterBackground that uploads data to my server, if the user has changed settings relevant to the push notifications. When the user changes a setting I set a static BOOL to YES and only send the changes when the app enters the background. I pass the a block ending the task to the method so when reaching connectionDidFinishLoading it calls it and ends the task.
It works most the times on the simulator, but doesn’t work on the actual device.
Relevant code:
self.bgTask = [application beginBackgroundTaskWithExpirationHandler:^
{
[application endBackgroundTask:self.bgTask];
self.bgTask = UIBackgroundTaskInvalid;
}];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^
{
[PushInfo checkDirty:^{
NSLog(@"push info sent");
[application endBackgroundTask:self.bgTask];
self.bgTask = UIBackgroundTaskInvalid;
}];
});
...
// in PushInfo.m :
typedef void (^VoidBlock)();
static BOOL dirty;
+ (void) checkDirty:(VoidBlock)endBlock
{
if(dirty)
{
PushInfo *pi = [[PushInfo alloc] init];
NSLog(@"sending pushinfo"); // This code is always reached
[pi setEndBlock:endBlock];
[pi updatePushInfo];
}
else
endBlock();
}
- (void) updatePushInfo
{
...
// Create a NSURLConnection to send the data
...
}
- (void) connectionDidFinishLoading:(NSURLConnection *)connection
{
...
NSLog(@"sent push info");
dirty = NO;
if(endBlock)
{
endBlock();
}
}
Am I missing anything ?
EDIT : even when it does send the information to the server on the simulator, the static variable is still YES for some reason…
ended using +sendSynchronousRequest:returningResponse:error: