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
(opens in a new tab), which coordinates the reading and writing of files and directories among multiple processes and objects in the same processNSFilePresenter
(opens in a new tab), a protocol that should be implemented by objects that allow the user to view or edit the content of files or directories
NSFileCoordinator
(opens in a new tab) essentially represents a cross-process file-locking mechanism, while NSFilePresenter
(opens in a new tab) offers callbacks that can be used to refresh your application state due to external file changes.
File coordination and Nutrient
Nutrient iOS SDK 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
(opens in a new tab) and implements NSFilePresenter
(opens in a new tab) callbacks that act on external file modifications.
Using file coordination in Nutrient
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 Nutrient.
Disabling 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:
- On a case-by-case basis, using
Document
via itsDocument(dataProviders:)
initializer, by passing a regularFileDataProvider
instance instead of aCoordinatedFileDataProvider
. - By changing the default framework behavior during Nutrient 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 Nutrient to refresh, like this:
pdfViewController.document?.clearCache()pdfViewController.reloadData()
[pdfViewController.document clearCache];[pdfViewController reloadData];
PSPDFFileCoordinationDelegate
FileCoordinationDelegate
is a replacement for NSFilePresenter
(opens in a new tab)-like callbacks on the Document
level. Since a Document
can be composed of several data providers (and hence several CoordinatedFileDataProvider
s), it doesn’t implement NSFilePresenter
(opens in a new tab) 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
(opens in a new tab) defines addFilePresenter(_:)
and removeFilePresenter(_:)
for doing this. In addition, Nutrient 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 Document
s 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 Nutrient 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
Nutrient 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. Consult the conflict resolution guide for more information.