7

My app supports background location updates (specifically with significant location monitoring).

Do I need to prevent UI (via controllers etc.) from loading when I identify that the app is in background (application.applicationState == UIApplicationStateBackground)?

My intention is to avoid heavy UI loading (it's a BIG app) in background, that might waste all the limited time I have in background to actually respond to the location update.

For example (in ObjC, but the question is also for Swift), let's say I have some RootViewController which initializes and holds the whole controllers/view hierarchy, should I, in the viewDidLoad do:

if ([UIApplication sharedApplication].applicationState ==  UIApplicationStateBackground) {
    // Root view controller loaded while in background, so doing only background stuff
    [self justDoBackgroundStuffWithoutUI];
} else {
    // Root view controller loaded normally, so loading normal UI
    [self initializeUIAndChildControllers];
}

? Or should I just "trust" iOS to ignore all of those UI tasks, because it will know it's in a background state?

mllm
  • 17,068
  • 15
  • 53
  • 64
  • 2
    You can just trust iOS to handle this, it wont render UI in a background state, thats built into the os. The underlying UIKit framework will decide whether the application needs to actually render or not. Bear in mind that application lifecycle events for view controllers are mostly in response to user interaction, once viewDidLoad and viewDidAppear have been run the rest only really runs when the user does something, which they wont be whilst the app is not in the foreground – Scriptable Jan 05 '17 at 11:00
  • Another thought then is what if my controllers also for example download many images automatically on their `viewDidLoad` - that, I'm guessing, won't be ignored by the os. Maybe the `UIImageView`a won't be rendered, but the data and `UIImage`s will indeed be loaded. – mllm Jan 05 '17 at 11:17
  • In that case the network requests 'should' still complete, but when an applciation is backgrounded any remaining tasks only have a limited amount of time to complete what they are doing before being suspended/terminated. it won't just keep running indefinately. – Scriptable Jan 05 '17 at 11:19
  • 1
    @mllm: Try moving the image downloading code to viewWillAppear (but only perform it once obviously). viewWillAppear shouldn't be called until just before the view actually appears. Also, with iPad Pro + split screen, your app may be in the background but visible, so you might have to do screen updates while in the background. – gnasher729 Jan 05 '17 at 12:21

2 Answers2

4

You can ignore those and let the OS handle that, just be careful if you have long running BG tasks, they may or may not have time to complete so it is best to very careful as it won't allow you to run tasks forever.

Matt Douhan
  • 2,053
  • 1
  • 21
  • 40
1

Updating UI in background is unnecessary and confusing and non-efficient.

  • unnecessary because there is no added value to the user who's not using the app. A simple doing of things in viewWillAppear would suffice.
  • confusing because users maybe expecting to see changes themselves as the screen is appeared, I don't know of your app, but this is more of a business choice. Maybe you have something similar in the manner of displaying changes like gmail/whatsapp and you want users to see the new emails/messages.
  • non-efficient because you are just doing something battery consuming too early. You even said "heavy UI loading". What happens if the location changes again and again and again? could the changes be overridden in the sense that they are no longer necessary or you will always need the changes regardless.

To summarize: I'm not saying don't do any UI updates, every app has a sweet spot. Likely you won't need to do most of them and lazy loading the changes ie upon loading screen seems to be the better way. Though I'm sure there are some advanced guidelines which I'm unaware of. Hopefully other answers would will come.

mfaani
  • 33,269
  • 19
  • 164
  • 293