applicationWillEnterForeground 方法在切回后台后点击 App Icon 切回前台没有回调
来源:互联网 发布:白金数据txt 编辑:程序博客网 时间:2024/05/24 03:59
在 Stack Overflow 这个问答中找到了答案:https://stackoverflow.com/questions/15405442/applicationdidenterbackground-and-applicationwillenterforeground-method-are-not
Question:
I need a long running task to be done in background as well as in foreground. This updates the core data. So to maintain UI responsive I created an another thread where I use different managedObjectContext(MOC). So a timer is set in background as well as in foreground and is inactivated appropriately when state changes. Before the task is starting and after the task is completed when I press home button it calls the two delegate methods properly but during the task is active when I press home button screen changes and UI hangs (becomes blank) but the two delegate methods are not called properly and the app is not terminated. I could not find the reason why this happens so. It would be helpful if someone can help.
I will attach the required code with this :
-(void) startTimerThread{ dispatch_async( dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{ // Add code here to do background processing NSManagedObjectContext *context = [[NSManagedObjectContext alloc] init]; [context setPersistentStoreCoordinator:[(AppDelegate *)[[UIApplication sharedApplication] delegate] persistentStoreCoordinator]]; self.managedObjectContext = context; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(mergeChanges:) name:NSManagedObjectContextDidSaveNotification object:context]; NSLog(@"managedObjContext : %@\n",self.managedObjectContext); [self getDataFromFile]; dispatch_async( dispatch_get_main_queue(), ^{ // Add code here to update the UI/send notifications based on the // results of the background processing [[NSNotificationCenter defaultCenter] postNotificationName:@"ReloadAppDelegateTable" object:nil]; [context release]; [[NSNotificationCenter defaultCenter] removeObserver:self name:NSManagedObjectContextDidSaveNotification object:context]; }); });}- (void)applicationDidEnterBackground:(UIApplication *)application{ NSLog(@"Background\n"); [self.notificationTimer invalidate]; self.notificationTimer = nil; UIApplication *app = [UIApplication sharedApplication]; self.bgTask = [app beginBackgroundTaskWithExpirationHandler:^{ [app endBackgroundTask:bgTask]; bgTask = UIBackgroundTaskInvalid; }]; //start location update timer and background timer self.timer = [NSTimer scheduledTimerWithTimeInterval:180 target:self selector:@selector(startLocationServices) userInfo:nil repeats:YES]; self.locationManager.delegate = self; [self.locationManager startUpdatingLocation]; self.logDownloader.managedObjectContext = self.managedObjectContext; NSLog(@"managedObjContext : %@\n",self.logDownloader.managedObjectContext); self.backgroundTimer = [NSTimer scheduledTimerWithTimeInterval:90 target:self.logDownloader selector:@selector(getDataFromFile) userInfo:nil repeats:YES];}- (void)applicationWillEnterForeground:(UIApplication *)application{ NSLog(@"Foreground\n"); //invalidate background timer and location update timer [self.timer invalidate]; [self.backgroundTimer invalidate]; self.timer = nil; self.notificationTimer = nil; self.logDownloader.managedObjectContext = self.managedObjectContext; NSLog(@"managedObjContext : %@\n",self.logDownloader.managedObjectContext); [[NSNotificationCenter defaultCenter] postNotificationName:@"ReloadAppDelegateTable" object:nil]; self.notificationTimer = [NSTimer scheduledTimerWithTimeInterval:180 target:self.logDownloader selector:@selector(startTimerThread) userInfo:nil repeats:YES];}
Answer:
The reason why applicationDidEnterBackground:
and applicationDidEnterForeground:
are never called is because these methods are used in joint with Application does not run in background
this option can be found in your ***-info.plist
. If this option is set to YES
than your app will never call these methods, because these when you press the home button with an app that has set the option to YES
the instance of the app that is running will get terminated so everytime you press the home button and then select the app icon a new instance is being created so it is using applicationWillTerminate:
.
The methods that Kirti mali
has said would also be the incorrect methods to use for want you are after, the reason being is that applicationDidBecomeActive:
and applicationWillResignActive:
are used when something like when you answer a phone call. The instance running is not terminated neither is it sent to the background. The instance is paused until the user has finished on that call when it will become active again.
So the solution to this would be if you want the app to run in background would be to change the option "Application does not run in background
" in the ***-info.plist to be
NOjust
applicationDidBecomeActive:and
applicationWillResignActive:` is the wrong way for these methods to be used.
Please see the apple documentation on UIApplicationDelegate
to get a better understanding of these methods.
- applicationWillEnterForeground 方法在切回后台后点击 App Icon 切回前台没有回调
- 键盘显示的情况下,按home进入后台,再点击app图标切换回前台时发生crash
- MBProgressHUD导致viewController在进入后台再进入前台后点击无效
- ajax访问后台后前台永远执行error的回调函数
- 6种方法来判断App处于前台还是后台
- 判断App位于前台或者后台的6种方法
- Android APP监听前台后台转换的方法
- Asp.net前台调后台方法
- 后台调前台方法之 ajax
- 后台调用前台方法
- 前台调用后台方法
- 前台调用后台方法
- 前台调用后台方法
- 前台调用后台方法
- 微信APP支付,后台回调
- Android 点击图标使APP由后台切换至前台重新启动欢迎页的问题
- 后台返回前台list,前台ajax接收后遍历的方法
- 在Struts2中jsp前台传值到action后台的方法
- webdriver 多窗口切换
- 通过鼠标的移动让图片移动
- render_to_response() got an unexpected keyword argument 'context_instance'
- HTTP的长连接和短连接
- 案例分析Java语言中try-catch-finally过程
- applicationWillEnterForeground 方法在切回后台后点击 App Icon 切回前台没有回调
- 2017-2018 ACM-ICPC, NEERC, Southern Subregional Contest E. Field of Wonders
- 【关于电脑】
- 从用剑到剑术——我的编程学习之路
- 计算闰年,自定义10000年以内的。数从8772起,求教?
- Opencv Mat矩阵中data、size、depth、elemSize、step等属性的理解
- Selenium
- 杨辉三角java
- python之实战----线性判别分析(LDA)战iris