Ios 当应用程序也在后台时,从后台线程更新UI,以便在下一份简历上显示启动屏幕

Ios 当应用程序也在后台时,从后台线程更新UI,以便在下一份简历上显示启动屏幕,ios,nstimer,splash-screen,background-thread,Ios,Nstimer,Splash Screen,Background Thread,当应用程序在后台停留超过3分钟时,我想显示一个启动屏幕。我启动了后台计时器来触发处理UI更新的函数 - (void)applicationDidEnterBackground:(UIApplication *) if (application.applicationState == UIApplicationStateBackground) { UIBackgroundTaskIdentifier bgTask = UIBackgroundTaskInvalid; UIAppli

当应用程序在后台停留超过3分钟时,我想显示一个启动屏幕。我启动了后台计时器来触发处理UI更新的函数

- (void)applicationDidEnterBackground:(UIApplication *)
if (application.applicationState == UIApplicationStateBackground) {
    UIBackgroundTaskIdentifier bgTask = UIBackgroundTaskInvalid;
    UIApplication *app = [UIApplication sharedApplication];
    bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
        [app endBackgroundTask:bgTask];
    }];
    //and create new timer with async call:
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
        splashTimer = [NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(timerFired) userInfo:nil repeats:NO];
        [[NSRunLoop currentRunLoop] addTimer:splashTimer forMode:NSDefaultRunLoopMode];
        [[NSRunLoop currentRunLoop] run];
    });
}
}
现在,当定时器被调用时,我调用函数来更新我的UI,即在窗口中添加一个图像,使其看起来像一个启动屏幕

  - (void)timerFired
    {
      [splashTimer invalidate];
        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
            UIImageView *splash = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Splash_Screen.png"]];
            splash.frame = self.window.bounds;
            [self.window addSubview:splash];
        }];
}
然后当应用程序再次进入前台时,从窗口中删除此图像

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    if ([[self.window subviews] count] > 1) {
        [NSThread sleepForTimeInterval:1.0];
        [[[self.window subviews] lastObject] removeFromSuperview];
    }
}

但是当我从后台线程调用它时,我的启动屏幕并没有显示出来。我什么都试过了,但运气不好。我的问题是,我的计时器是从后台线程调用的,如果我想从这个线程添加图像,UI不会更新。有任何帮助吗?

当所有后台任务过期/结束时,Springboard不会更新应用程序的快照[1]。IIRC当应用程序处于后台时,快照仅在处理这些后台事件后应用程序挂起之前更新:

  • 后台提取/刷新
  • 远程通知
  • (我不记得第三个了。可能是后台下载?)
[1] 当应用程序移回前台时,出现在任务切换器中的图像