File coordination encompasses a set of system APIs and callbacks that allow you to coordinate file access safely between different processes or different threads.
It consists of two main system APIs:
NSFileCoordinatorwhich coordinates the reading and writing of files and directories among multiple processes and objects in the same process.
NSFilePresentera protocol that should be implemented by objects that allow the user to view or edit the content of files or directories.
PSPDFKit for iOS supports file coordination out of the box since version 6.7.0. Coordinated file access is handled on the data provider level, using
PSPDFCoordinatedFileDataProvider is a subclass of our standard
PSPDFFileDataProvider, which is used to read and write data from/to a PDF file on disk. The coordinated subclass adds coordinated file access using
NSFileCoordinator and implements
NSFilePresenter callbacks that act on external file modifications.
If you are using any of the standard
PSPDFDocument initializers that take a file URL as input (such as
documentWithURL:), then good news, there is nothing you need to do. You are already using file coordination.
PSPDFDocument will automatically create a
PSPDFCoordinatedFileDataProvider behind the scenes when it receives a file URL during initialization.
If you are initializing the document using an explicit data provider that acts on files (via
documentWithDataProvider:), then we recommend switching to
PSPDFCoordinatedFileDataProvider or a subclass of it.
In most cases, it is recommended to use the default behavior and keep file coordination turned on for all files. File coordination is important whenever there is a chance that multiple processes (or threads inside your app) could be accessing the same file on disk at the same time. That might happen if you are providing extensions, using iCloud, using our FTS library indexing, or simply executing multithreaded code. If you are however sure, that this is not something that affects you, or if you are experiencing problems related to file coordination, you can opt to not use file coordination inside your app.
You can disable file coordination in two ways.
documentWithDataProvider:initializer, passing a regular
PSPDFFileDataProviderinstance instead of a
PSPDFileCoordinationDelegate is a replacement for
NSFilePresenter-like callbacks on the
PSPFDDocument level. Since a
PSPDFDocument can be composed out of several
PSPDFDataProviders (and hence several
PSPDFCoordinatedFileDataProviders) it doesn't implement
NSFilePresenter directly. Instead the coordinated providers forward the file presenter calls to the document using
PSPDFileCoordinationDelegate callbacks. You can override those callbacks in your subclasses, if you need perform custom behaviors in response to document update. If you do so, be sure to call
super to ensure appropriate default behaviors are also invoked by
File presenters need to be enabled from UI classes when they become active, and deactivated when UI presentation ends.
removeFilePresenter: to do so. To help with this, PSPDFKit adds
PSPDFFilePresenterCoordinator, which defines helpers for registering multiple file presenters at the same time and takes care of automatically temporarily unregistering file presenters while the application is in the background. Failing to do so risks, according to Apple documentation, system wide deadlocks.
and when ending document presentation:
Be sure to also implement
PSPDFDocumentUnderlyingFileChangedNotification to receive file coordination changes from the presented document(s).
Built-in PSPDFKit controllers (
PSPDFMultiDocumentViewController) already implement
PSPDFFilePresenterCoordinator methods when setting and changing documents, so if you are using or subclassing them, there is nothing you need to do in this regard. They also implement
PSPDFDocumentUnderlyingFileChangedNotification and automatically reload the UI or remove the document, depending on the change type.