i am using following code
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 40000
if ([[UIApplication sharedApplication]
respondsToSelector:@selector(beginBackgroundTaskWithExpirationHandler:)])
{
UIBackgroundTaskIdentifier bgTask = [[UIApplication sharedApplication]
beginBackgroundTaskWithExpirationHandler:^{}];
// Perform work that should be allowed to continue in background
[self changeCounter];
//[NSThread detachNewThreadSelector:@selector(changeCounter) toTarget:self withObject:_viewController];
[[UIApplication sharedApplication] endBackgroundTask:bgTask];
}
#endif
changeCounter contains loops which may end after some time .But before ending the loop if app comes in foreground then i can see only black screen untill loop finishes.
So how can I stop all tasks as app comes in foreground
this is code for changeCounter
-(void)changeCounter{
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
while(back==1.0f){
NSLog(@"loop is runnig");
[NSThread sleepForTimeInterval:1.0];
if(_viewController.minutes<0){
if(_viewController.fadeSeconds>0){
float div=_viewController.deviceVolume/[_viewController.volumeData.fadeTime floatValue];
_viewController.musicPlayer.volume=_viewController.musicPlayer.volume-div;
_viewController.fadeSeconds-=1;
}
else {
[self stop];
//self.musicPlayer.volume=0.0f;
// counter=0;
NSLog(@"closing the sound");
[_viewController.musicPlayer pause];
NSLog(@"fade seconds %i minutes ",_viewController.fadeSeconds);
if(_viewController.dvol==0){
_viewController.musicPlayer.volume=deviceVolume;
_viewController.dvol=1;
}
back=0.0f;
}// end of the else
}
I think that your [self changeCounter] method is running on your application’s main thread which is why your seeing the black screen until the operation is finished. You should think about whether it is appropriate for that operation to run on the main thread or whether you should move it to a background thread. Do you really want to kill the task when you come back to the foreground or is it ok for the task to complete as long as the UI is not locked up?