NSFileCoordinator/Presenter and background location apps

Dear Experts,

It is necessary to remove NSFilePresenters when an app goes to the background and re-add them when it returns to the foreground - if you don't do so, the app will be terminated.

But what about an app that has the location background mode enabled?

Specifically, I have an app that records your location, in the foreground or background, and writes this data to a file. It does writes to the file inside a coordinated write block, and monitors for writes from other processes. This works fine in the foreground. I'd really like to also get notifications of concurrent changes to the file while the app is in the background, but this doesn't seem to be possible.

(It's actually more likely that a file will be modified by another app while I'm in the background - consider switching to the Files app, and then back again.)

Any suggestions?

It's actually more likely that a file will be modified by another app while I'm in the background

Suspended in the background? Or running in background?

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

It's actually more likely that a file will be modified by another app while I'm in the background

Suspended in the background? Or running in background?

This question is about the case where my app is running in the background to receive location updates.

Hmmm, “location updates” is ambiguous because the location system can run in two modes:

  • Continuous updates, like driving directions, which prevent you from suspending

  • Triggered updates, like region monitoring, which suspend you between updates

Regardless, my expectation is that the file coordination system should work fine in the background as long as you’re not suspended. If you are suspended, it won’t resume (or relaunch) you.

I’m not aware of any subsystem then will resume (or relaunch) you in response to file system changes.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

I'm only interested in continuous location updates.

my expectation is that the file coordination system should work fine in the background as long as you’re not suspended.

Thanks. A quick check finds that indeed I can leave the NSFilePresenter in place and I do get a notification if another process (e.g. the Files app) deletes the file while I am running in the background. I do get a warning in the console:

File presenter <....> with presentedItemURL .... was still registered at the time this application was suspended, and implements one or more NSFilePresenter messages requiring a response. For NSFilePresenters for file system locations that are accessible to other processes (e.g. iCloud or group containers), you should either call removeFilePresenter: when the process is backgrounded, or remove any implementations of NSFilePresenter methods requiring a response. Otherwise, the system will kill your process instead of risking deadlock.

I can't do this unconditionally though - sometimes the app will be recording locations and so will be running in the background, but other times it will not be recording locations and will be suspended. I guess I can track whether I am getting location updates and remove the file presenter if I'm not. Or, is there some other e.g. UIApplicationDelegate method that I should use rather than applicationWill/DidEnterBackground? Or can I ask iOS, from the will/didEnterBackground method, whether or not I will be suspended?

I guess I can track whether I am getting location updates and remove the file presenter if I'm not.

Right. You are effectively tracking your app’s “eligible for suspension” state. Unfortunately the OS does not provide any infrastructure for that; you have to do it yourself.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Thanks. This all feels a bit fragile but I think I can make it work.

I'm cautious though, because I don't want to discover that in some future iOS version the behaviour changes and I get terminated because I've not removed the NSFilePresenter when I background. Users would be unhappy to find that their track recording had stopped when they put their phone in their backpack.

NSFileCoordinator/Presenter and background location apps
 
 
Q