NSFileCoordinator is not safe when developing app extensions

Originator:atomicbird
Number:rdar://19103850 Date Originated:01-Dec-2014 12:15 PM
Status:Open Resolved:
Product:iOS Product Version:8.x
Classification:Serious Bug Reproducible:Always
 
Summary:
iOS apps that have extensions frequently need to share data with those extensions. To avoid data corruption, coordinated access is required.

Fortunately iOS provides an API for coordinated access to files, via NSFileCoordinator and NSFilePresenter.

Sadly, these classes are specifically documented as being unsafe for use in this situation, e.g. in TN 2048 at https://developer.apple.com/library/ios/technotes/tn2408/_index.html

This is, to say the least, extremely unfortunate. This would seem to be a textbook example of a situation where NSFileCoordinator and NSFilePresenter would be ideal. Nay, it would seem to be the primary purpose for those classes to exist. The documentation for NSFileCoordinator even goes so far as to say that "...NSFileCoordinator class coordinates the reading and writing of files and directories among multiple processes." But actually using it between different processes turns out to not be safe.

The tech note explains that "...you are obliged to write to that container in a coordinated manner to avoid data corruption. However, you must not use file coordination APIs directly for this." This kind of documentation makes me sad.

Steps to Reproduce:
1. Develop an iOS app that has an extension which shares data with the containing app.
2. Use NSFileCoordinator and NSFilePresenter to coordinate data access between the app and its extension.

Expected Results:
Coordinated access would ensure safe access to data with no other ill effects.

Actual Results:
Attempting to use the file coordination API to coordinate file access may result in deadlocks.

Version:
Any version of iOS 8 (8.1.1 is current).

Notes:
The fact that this is documented does not make it any less of a bug.

Configuration:
Any iOS device

Update:

To add to this, WatchKit docs directly contradict the TN, and apparently recommend unsafe behavior.

WatchKit docs state the following at https://developer.apple.com/library/prerelease/ios/documentation/General/Conceptual/WatchKitProgrammingGuide/DesigningaWatchKitApp.html#//apple_ref/doc/uid/TP40014969-CH3-SW1

"Always use file presenters and coordinators to access files in a shared container directory. File presenters and coordinators allow for synchronized access to files and directories. Without them, your WatchKit extension and iOS app might make conflicting changes and corrupt the data they share."

WatchKit apps are a special kind of iOS extension. Tech Note 2048 says the following:

"Using file coordination in an app extension to access a container shared with its containing app may result in a deadlock."

Experience with app extensions suggests that the tech note is correct, that deadlocks are likely, and that this is extremely bad advice.

Comments


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!