iOS background fetch on app launch causes black screen

814 views Asked by At

I have been successfully using background fetch in the iOS app I'm developing, but I've noticed a strange bug that I'm having trouble squashing.

The initial symptom was that sometimes when I launched the app, the launch image would show, and then be replaced by a black screen. If I hit the home button and then reopened the app, all would be fine and the app would function as intended. I also noticed that this only seemed to occur if I hadn't used the app in a while. After adding a bunch of logging, I came to realize that the black screen on launch only occurred if the OS triggered a background fetch on launch. It seems that if a fetch hasn't been done in a while, and then you launch the app, iOS will trigger a "background" fetch on your currently launching app. The bug only occurs in this situation.

I did some googling and it seemed like one cause for this sort of behavior can be if you trigger animations at the wrong point in the app lifecycle. I did an audit of my app, and didn't find much. I also found this Stack Overflow post which describes a similar problem, except that my app is not crashing, and I'm not doing anything with background threads in my application:performFetchWithCompletionHandler: implementation.

I figured the next step would be to come up with a minimal test case that still causes the issue. So, using Xcode 6.1, I created a blank Single View Application (Swift). I then did the small amount of work to add in background fetching:

  • Added "Background fetch" to the Background Modes section on the Capabilities screen for my target.
  • Updated AppDelegate.swift to include the relevant background fetch pieces.

    import UIKit
    
    @UIApplicationMain
    class AppDelegate: UIResponder, UIApplicationDelegate {
    
        var window: UIWindow?
    
        func application(application: UIApplication,
            didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
                application.setMinimumBackgroundFetchInterval(UIApplicationBackgroundFetchIntervalMinimum);
                return true
        }
    
        func application(application: UIApplication!,
            performFetchWithCompletionHandler completionHandler: ((UIBackgroundFetchResult) -> Void)!) {
                completionHandler(UIBackgroundFetchResult.NewData);
        }
    }
    

I made no other changes, and then launched the app on the device. To get the situation to occur, I ran a number of Simulate Background Fetch commands from Xcode, stopped the app inside Xcode, and then waited a while (around 10 minutes). I then relaunched the app from Xcode and experienced the same behavior that was occurring in my actual app.

At this point I'm having trouble not concluding that this is some kind of bug in the SDK. Has anyone else seen this situation? Is there a workaround for it?

0

There are 0 answers