简体   繁体   中英

Will iOS kill my app if it becomes unresponsive?

I'm developing an iOS app for an office lobby installation. The app will only be installed on six iPads, and won't ever be distributed via the app store. The app will need to download a large content file once a day, and any time it's restarted. Also, the app will need to preload a bunch of images so they can be displayed quickly. This whole process takes a while -- 45 seconds or so. It's long enough so that the os kills the app on startup. I've deferred the loading process until after startup, which seems to work.

What I'm wonder now is whether I might run in to another OS-imposed time constraint, where if an iteration of the run loop takes too long to return (and the OS observes the app as being unresponsive) it kills it. Does such a constraint exist? Or, for my specific case, where I don't need to worry about annoying users with an unresponsive UI, can I safely take as long as I want to execute a task on the main thread, once the app has started.

Yes. The watchdog will kill your app if you block the main thread and the application takes too long to respond to system events. I don't believe that Apple state an actual time though.

More information here https://developer.apple.com/library/ios/#technotes/tn2008/tn2151.html or google 0x8badf00d . This is the exception code associated with a watchdog timeout.

Technically, you can run long-running task synchronously once the app has been launched if your users don't mind waiting for an unresponsive UI. However, a user may lock their device and unlock it later. If your app will fail to respond to application life-cycle callbacks ( applicationWillResignActive: and applicationDidBecomeActive: ) it still might get killed.

See this Q/A for technical information about the iOS watchdog -- a daemon process that makes sure that apps behave well.

Note that when you launch your app from Xcode, watchdog won't kill it no matter what. But once you've launched your app from the Home screen, watchdog takes over it. It has nothing to do with App Store actually.

To be safe, I would suggest you to perform long-running tasks in a background thread. This is very easy to do with [self performSelectorInBackground:withObject:] . And you can show an activity indicator or even display the number of downloaded files in the UI so that users know something is working under the hood.

To update UI from the background thread, you have to call to the main thread to execute the update code. It can be done like this:

- (void)methodRunningInBackground
{
    // some work is done here ...

    dispatch_async(dispatch_get_main_queue(), ^{
         // update your UI here
    });
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM