【问题标题】:iOS7 wrong screenshot in app switcher while in backgroundiOS7后台应用切换器截图错误
【发布时间】:2014-06-29 07:50:17
【问题描述】:

当我的应用程序正在下载大文件并且用户切换到另一个应用程序时,我正在运行这样的后台任务:
beginBackgroundTaskWithExpirationHandler:
然后如果用户通过双击打开“应用程序切换器”,我的应用程序的屏幕截图是完全随机的。有时它显示的视图控制器甚至没有在应用程序中打开。

ignoreSnapshotOnNextApplicationLaunch 没有帮助,因为它根本不起作用。

Apple 说:Avoid updating your windows and views 这里:documentation,但我不会更新视图。

我也在运行计时器,以检查剩余多少后台时间,而这个计时器是我问题的原因。如果我不创建它,一切都会完美运行,但我无法在 Expiration 处理程序中保存下载状态 - 时间不够。
我怎样才能避免这种奇怪的行为?

-(void)appDidEnterBackground {
    UIApplication *application = [UIApplication sharedApplication];
    __block UIBackgroundTaskIdentifier bgTask;
    bgTask = [application beginBackgroundTaskWithExpirationHandler:^{
        [application endBackgroundTask:bgTask];
        bgTask = UIBackgroundTaskInvalid;
    }];

    if(backgroundTimer == nil || ![backgroundTimer isValid]) {
        backgroundTimer = [[NSTimer alloc]
                initWithFireDate:[NSDate dateWithTimeIntervalSinceNow:0]
                        interval:1
                          target:self
                        selector:@selector(checkBackgroundTimeRemaining)
                        userInfo:nil
                         repeats:YES];
        [[NSRunLoop currentRunLoop] addTimer:backgroundTimer forMode:NSRunLoopCommonModes];
        [[NSRunLoop currentRunLoop] run];
    }
}

- (void)checkBackgroundTimeRemaining {
    NSTimeInterval timeRemaining = [[UIApplication sharedApplication] backgroundTimeRemaining];

    if(timeRemaining < 5) {
        if(backgroundTimer != nil) {
            [backgroundTimer invalidate];
            backgroundTimer = nil;
        }
        [downloadTask cancelByProducingResumeData:^(NSData *resumeData) {
            [self saveResumeData:resumeData];
        }];
    }
}

【问题讨论】:

    标签: ios objective-c background-process


    【解决方案1】:

    有时它显示的视图控制器甚至没有在应用程序中打开。

    这听起来很可疑,不应该发生。也许你可以添加一些代码来显示你在做什么?

    ignoreSnapshotOnNextApplicationLaunch 在这里无关紧要,因为它仅用于确定当用户再次点击您的图标以打开应用程序时会发生什么。

    您是否可能忘记在完成后台任务时调用 endBackgroundTask:?

    我不确定你打算用计时器做什么?如果要确定您在后台执行的剩余时间,请改用 UIApplication 的 backgroundTimeRemaining。

    【讨论】:

    • 我现在已经摆脱了计时器。我正在检查下载另一部分数据时的剩余时间。仍然无法理解计时器如何影响应用切换器的工作。
    • 应用切换器仅显示您的应用进入后台时显示的最后一个视图。当您在后台获取时,它会在您调用 application:performFetchWithCompletionHandler: 中的完成处理程序后被捕获
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多