Commit 38e6f360 authored by QuentinArguillere's avatar QuentinArguillere
Browse files

Remove use of app.application state for better log message, as it was not thread safe

1 merge request!315Backport 5.3 - (IOS) Remove use of app.application state for better log message, as it was not thread safe
Showing with 36 additions and 44 deletions
......@@ -31,50 +31,42 @@ namespace bctoolbox {
unsigned long IOSUtilsApp::beginBackgroundTask(const char *name, std::function<void()> cb) {
__block UIBackgroundTaskIdentifier bgid = UIBackgroundTaskInvalid;
dispatch_block_t block = ^{
UIApplication *app=[UIApplication sharedApplication];
@try {
if (cb==nullptr){
bctbx_error("belle_sip_begin_background_task(): the callback must not be NULL. Application must be aware that the background task needs to be terminated.");
bgid = UIBackgroundTaskInvalid;
@throw([NSException exceptionWithName:@"LinphoneCoreException" reason:@"Background task has no callback" userInfo:nil]);
}
void (^handler)() = ^{
cb();
};
if([app respondsToSelector:@selector(beginBackgroundTaskWithName:expirationHandler:)]){
bgid = [app beginBackgroundTaskWithName:[NSString stringWithUTF8String:name] expirationHandler:handler];
} else {
bgid = [app beginBackgroundTaskWithExpirationHandler:handler];
}
if (bgid==UIBackgroundTaskInvalid){
bctbx_error("Could not start background task %s.", name);
bgid = 0;
@throw([NSException exceptionWithName:@"LinphoneCoreException" reason:@"Could not start background task" userInfo:nil]);
}
// backgroundTimeRemaining is properly set only when running background... but not immediately!
if (app.applicationState != UIApplicationStateBackground || (app.backgroundTimeRemaining == DBL_MAX)) {
bctbx_message("Background task %s started. Unknown remaining time since application is not fully in background.", name);
} else {
bctbx_message("Background task %s started. Remaining time %.1f secs", name, app.backgroundTimeRemaining);
}
}
@catch (NSException*) {
// do nothing
}
};
if( [NSThread isMainThread] ) {
block();
}
else {
dispatch_sync(dispatch_get_main_queue(), block);
}
UIApplication *app=[UIApplication sharedApplication];
@try {
if (cb==nullptr){
bctbx_error("belle_sip_begin_background_task(): the callback must not be NULL. Application must be aware that the background task needs to be terminated.");
bgid = UIBackgroundTaskInvalid;
@throw([NSException exceptionWithName:@"LinphoneCoreException" reason:@"Background task has no callback" userInfo:nil]);
}
void (^handler)() = ^{
cb();
};
if([app respondsToSelector:@selector(beginBackgroundTaskWithName:expirationHandler:)]){
bgid = [app beginBackgroundTaskWithName:[NSString stringWithUTF8String:name] expirationHandler:handler];
} else {
bgid = [app beginBackgroundTaskWithExpirationHandler:handler];
}
if (bgid==UIBackgroundTaskInvalid){
bctbx_error("Could not start background task %s.", name);
bgid = 0;
@throw([NSException exceptionWithName:@"LinphoneCoreException" reason:@"Could not start background task" userInfo:nil]);
}
// backgroundTimeRemaining is properly set only when running background... but not immediately!
// removed app.applicationState check because not thread safe
if (app.backgroundTimeRemaining == DBL_MAX) {
bctbx_message("Background task %s started. Unknown remaining time since application is not fully in background.", name);
} else {
bctbx_message("Background task %s started. Remaining time %.1f secs", name, app.backgroundTimeRemaining);
}
}
@catch (NSException*) {
// do nothing
}
return (unsigned long)bgid;
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment