Protection against deleting the file while downloading

Hello,

I am currently developing an iOS software that share files between devices through iCloud Documents.


1. „Device 1” sends a file to iCloud:

FileManager.default.setUbiquitous(true, itemAt: file, destinationURL: url)


2. „Device 2” detects the appearance of a file and then downloads it:

FileManager.default.startDownloadingUbiquitousItem(at: url!)


I would like to avoid the situation in which the „Device 1” will delete the file while it is being downloaded by the „Device 2”.


Is there any way to check on the „Device 1” that the file is being downloaded by the „Device 2”, or block the possibility of deleting the file while downloading?

Replies

Sure. Set up a field named "I am downloading the file, don't delete it" and another field that says "I am deleting the file, don't download it".


Device 2 looks for the value of the second field - if it is empty then it enters something in the first field and it downloads. When done, it clears the value in the first field


Device 1 looks for the value in the first field, if it is empty it sets the value in the second field and it deletes the file. Then it deletes both fields.

Thank you for the answer, I was hoping that there might be a ready mechanism blocking the possibility of deleting the file. I did it like you wrote, I used NSUbiquitousKeyValueStore. This is not an excellent solution because it takes some time to synchronize the keys. However, for the needs of this application it looks like a solution that is good enough.

Umm, whoops.


What I wrote above applies to CloudKit. Unfortunately with the key-value file there is an interloper that makes things more complicated, it is the eessence of 'ubiquitous' - and this may also be true of Documents, I don't know. There are three different versions of the key-value file. One in your app, one on your device and one in the Cloud. Synch aligns the version in your app with what is on your device. The system, as it decides in its infinte wisdom, aligns the device with the Cloud. You have no control over that. It can take a few seconds.

I am not aware of any function in iOS that would allow this.


In my view, there are two ways of dealing with this problem:

  1. The app that downloads the file must be able to deal adequately with the situation where a file has already been deleted before it has been fully downloaded to device 2.
  2. You have to negotiate (e.g. via the iCloud Key-Value Store) a kind of "owner" of a file between the participating devices. And only the owner of a file is allowed to delete it.

However, your app has to deal with the situation that a device is offline and therefore communication with other devices is not possible.