File Coordination on iOS

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:

  • NSFileCoordinator, which coordinates the reading and writing of files and directories among multiple processes and objects in the same process

  • NSFilePresenter, a protocol that should be implemented by objects that allow the user to view or edit the content of files or directories

NSFileCoordinator essentially represents a cross-process file-locking mechanism, while NSFilePresenter offers callbacks that can be used to refresh your application state due to external file changes.

File Coordination and PSPDFKit

PSPDFKit for iOS has supported file coordination out of the box since version 6.7.0. Coordinated file access is handled at the data provider level using CoordinatedFileDataProvider. CoordinatedFileDataProvider is a subclass of our standard FileDataProvider, which is designed 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.

Using File Coordination in PSPDFKit

If you are creating a Document using Document(url:), then there is nothing you need to do, because you are already using file coordination; Document will automatically create a CoordinatedFileDataProvider behind the scenes when it receives a file URL during initialization.

If you are initializing the document using an explicit FileDataProvider (using Document(dataProviders:)), then we recommend switching to CoordinatedFileDataProvider as soon as your application deals with PDF files that could be accessed by other processes or by code in your own application that lies outside of PSPDFKit.

Disable File Coordination

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. This might happen if you are providing extensions, using iCloud, using our FTS library indexing, or simply executing multithreaded code. However, if you are 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:

  1. On a case-by-case basis, using Document via its Document(dataProviders:) initializer, by passing a regular FileDataProvider instance instead of a CoordinatedFileDataProvider.

  2. By changing the default framework behavior during PSPDFKit initialization (.fileCoordinationEnabled), as shown below.

PSPDFKit.SDK.setLicenseKey(yourLicenseKey, options: [.fileCoordinationEnabled: false])
[PSPDFKitGlobal setLicenseKey:yourLicenseKey options:@{PSPDFSettingKeyFileCoordinationEnabled: false});

File coordination takes care of observing changes to the underlying file while it’s open in a Document or PDFViewController. If you disable file coordination, then when you make changes to the underlying file, your app is responsible for telling PSPDFKit to refresh, like this:

pdfViewController.document?.clearCache()
pdfViewController.reloadData()
[pdfViewController.document clearCache];
[pdfViewController reloadData];

PSPDFFileCoordinationDelegate

FileCoordinationDelegate is a replacement for NSFilePresenter-like callbacks on the Document level. Since a Document can be composed of several data providers (and hence several CoordinatedFileDataProviders), it doesn’t implement NSFilePresenter directly. Instead, the coordinated providers forward the file presenter calls to the document using FileCoordinationDelegate callbacks. You can override those callbacks in your subclasses if you need to perform custom behaviors in response to document updates. If you do so, be sure to call super to ensure appropriate default behaviors are also invoked by Document. The default implementation of FileCoordinationDelegate in Document asynchronously dispatches PSPDFDocumentUnderlyingFileChanged during both file changes and deletion.

Registering for File Presenter Callbacks

File presenters need to be enabled from UI classes when they become active, and they need to be deactivated when UI presentation ends. NSFileCoordinator defines addFilePresenter(_:) and removeFilePresenter(_:) for doing this. In addition, PSPDFKit adds FilePresenterCoordinator, 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, systemwide deadlocks.

If you are using Documents in your custom view controllers, you should use FilePresenterCoordinator to initiate file observation when presenting a Document:

FilePresenterCoordinator.shared.observe(document.filePresenters)
[PSPDFFilePresenterCoordinator.sharedCoordinator observeFilePresenters:self.document.filePresenters];

You should also do this when ending document presentation:

FilePresenterCoordinator.shared.unobserve(document.filePresenters)
[PSPDFFilePresenterCoordinator.sharedCoordinator unobserveFilePresenters:self.document.filePresenters];

Be sure to also implement PSPDFDocumentUnderlyingFileChanged to receive file coordination changes from the presented document(s).

Built-in PSPDFKit controllers (PDFViewController, MultiDocumentViewController, and PDFTabbedViewController) already implement FilePresenterCoordinator methods when setting and changing documents, so if you are using or subclassing them, there is nothing you need to do in this regard.

Responding to File Presentation Callbacks

PSPDFKit controllers, including PDFViewController, MultiDocumentViewController, and PDFTabbedViewController, are set up to listen to PSPDFDocumentUnderlyingFileChanged. Depending on the notification parameters, document state, and system capabilities, the controllers will either trigger appropriate actions to automatically respond to a file change or present a conflict resolution UI. Please consult the conflict resolution guide for more information.