How to access iCloud Documents files in an asynchronous manner

136 Views Asked by At

I am trying to add support for iCloud Documents in my existing app, but I am struggling badly with how to do that.

Apple seems to prefer that you use the UIDocument class for that. But UIDocument does not give direct access to the file in the file system, it expects to maintain a copy of the contents of the file in an NSData object instead. That is simply not doable in my case. All my current code and half of the 3rd party libraries that I use, work directly with the file on the file system, not with NSData. Rewriting all that code is simply not doable.

When not using the UIDocument class, Apple expects you to use the NSFileCoordinator to coordinate access to the file's contents. I am trying to do that with my code, but the methods on the NSFileCoordinator seem to expect that all reading and writing will be done in one synchronous sequence. All the methods of NSFileCoordinator take a block as argument, and expect all the reading/writing to be performed inside that block. When the block returns, you are not allowed to make any file access anymore, as far as I understand.

That is not doable in my case as well. Some of my code, and the 3rd party libraries, do the reading / writing in an asynchronous manner on background threads. I can identify the start and end of the period that the code needs access to the file contents, so if there was a separate requireAccess method, and separate relinquishAccess on the NSFileCoordinator, that would enable me to achieve the goal. But that does not seem to be the case.

It is unclear to me what the role of NSFilePresenter in this is. Some of the documentation, especially that of relinquishPresentedItemToReader() in NSFilePresenter seem to indicate that you can actually acquire / relinquish access separately:

If you want to be notified when the reader has completed its task, pass your own block to the reader and use that block to reacquire the file or URL for your own uses.

But it does not explain anywhere how to "reacquire" the file.

So the concrete question: I need to do the following steps in an asynchronous manner:

  1. acquire access to the file, and obtain a file: URL for the file on the local file system
  2. do multiple, asynchronous, read/write operations on the file with normal file system operations on that file: URL
  3. relinquish access to the file

Does anybody know whether it is possible to do this, and how to do step 1 and step 3 ?

0

There are 0 best solutions below