在iOS模拟器中按下主页按钮时不调用applicationDidEnterBackground和applicationWillEnterForeground方法 [英] applicationDidEnterBackground and applicationWillEnterForeground method are not called when pressed home button in iOS simulator

查看:1663
本文介绍了在iOS模拟器中按下主页按钮时不调用applicationDidEnterBackground和applicationWillEnterForeground方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在后台和前台完成一项长时间运行的任务。这会更新核心数据。因此,为了维护UI响应,我创建了另一个线程,我使用不同的managedObjectContext(MOC)。因此,计时器在后台和前台设置,并在状态发生变化时被适当地停用。在任务开始之前和任务完成之后,当我按下主页按钮时,它会正确调用两个委托方法但在任务处于活动状态时按下主页按钮屏幕更改并且UI挂起(变为空白)但两个委托方法不是正确调用,应用程序不会终止。我找不到这种情况发生的原因。如果有人可以提供帮助,将会很有帮助。

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.

我将附上所需的代码:

-(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];
}


推荐答案

之所以 applicationDidEnterBackground: applicationDidEnterForeground: 从不被调用是因为这些方法与应用程序无法在后台运行 此选项可在 *** - info.plist 中找到。如果此选项设置为 YES ,那么您的应用永远不会调用这些方法,因为当您按下主页按钮时,应用程序已将选项设置为 YES 正在运行的应用程序实例将被终止,因此每当您按下主页按钮然后选择应用程序图标时,正在创建新实例,因此它正在使用 applicationWillTerminate :

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:.

Kirti mali 所说的方法也是不正确的方法,用于你想要的,原因是 applicationDidBecomeActive: applicationWillResignActive: 用于回答问题的时候电话。运行的实例不会被终止,也不会被发送到后台。该实例暂停,直到用户完成该呼叫时它将再次变为活动状态。

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.

因此,如果您希望该应用在后台运行,则解决此问题将 *** - info.plist中的选项应用程序无法在后台运行更改为只需 applicationDidBecomeActive: applicationWillResignActive:`这些方法的错误方法是使用。

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 beNOjustapplicationDidBecomeActive:andapplicationWillResignActive:` is the wrong way for these methods to be used.

请参阅 UIApplicationDelegate ,以便更好地了解这些方法。

Please see the apple documentation on UIApplicationDelegate to get a better understanding of these methods.

这篇关于在iOS模拟器中按下主页按钮时不调用applicationDidEnterBackground和applicationWillEnterForeground方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆