Reputation: 538
beginBackgroundTaskWithExpirationHandler
creates a new thread when we execute something.
Can I execute something using an existing thread with this?
Because new thread generated by beginBackgroundTaskWithExpirationHandler
causes some problems to my application when it is resumed. So I passed an instance of an existing thread to beginBackgroundTaskWithExpirationHandler
and called the required methods using the existing thread. Is it ok to use existing threads inside beginBackgroundTaskWithExpirationHandler
?
Will it cause any problems?
- (void)applicationDidEnterBackground:(UIApplication *)application {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
IOSMobilePOSApplication *app = [IOSMobilePOSApplication getInstance];
if ([app keepAliveMessage]) {
if([[UIDevice currentDevice] respondsToSelector:@selector(isMultitaskingSupported)])
{
[Logger log:@"Multitasking Supported"];
background_task = [application beginBackgroundTaskWithExpirationHandler:^ {
[Logger log:@"Background maximum time exeeded."];
IOSMobilePOSApplication *iosMobileApplication = [IOSMobilePOSApplication getInstance];
[[iosMobileApplication getKeepAliveManager] stop];
//Clean up code. Tell the system that we are done.
[application endBackgroundTask: background_task];
background_task = UIBackgroundTaskInvalid;
}];
//To make the code block asynchronous
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
//### background task starts
[Logger log:@"Running in the background"];
IOSMobilePOSApplication *iosMobileApplication = [IOSMobilePOSApplication getInstance];
[[iosMobileApplication getKeepAliveManager] setEnabled:true];
[[iosMobileApplication getKeepAliveManager] performSelector:@selector(run) onThread:[[iosMobileApplication getKeepAliveManager] runingThread] withObject:NULL waitUntilDone:NO];
isStarted = true;
//#### background task ends
//Clean up code. Tell the system that we are done.
[application endBackgroundTask: background_task];
background_task = UIBackgroundTaskInvalid;
});
}
else
{
[Logger log:@"Multitasking Not Supported"];
}
}
}
here
[[iosMobileApplication getKeepAliveManager] run];
causes new thread to start and it causes thread synchronization problems in my code. So I added the code in place of above mentioned line.
[[iosMobileApplication getKeepAliveManager] performSelector:@selector(run) onThread:[[iosMobileApplication getKeepAliveManager] runingThread] withObject:NULL waitUntilDone:NO];.
Will this cause any problems when the app goes background?
Upvotes: 0
Views: 827
Reputation: 2047
It’s probably some mistake, because -beginBackgroundTaskWithExpirationHandler:
doesn’t create any threads. It only marks the beginning of some long-running task you’re about to start or just started.
This method can be called from any thread. The expiration handler that it takes as a parameter is called on the main thread. This handler is used to clean up and mark the end of the long-running task you’re doing.
Upvotes: 1