7

My application uses iCloud. My application stores all its data in 1 document and there should never be more than this 1 document. However, sometimes my NSMetadataQuery returns 2 documents:

0: Documents/(A Document being saved bt MyApp)/MyDoc.ext
1: Documents/MyDoc.ext

This happens mostly when the user opens the app shortly after the iOS device was rebooted.

When inspecting these 2 docs, it is document 0 that is the one with the most recent updates, so this is the one we want.

What is the reason of this "A Document Being Saved" ending up there and being the most recent one and how do I deal with this correctly, so Documents/MyDoc.ext will be the one that has the most recent content.

Thanks, -r

Stuart
  • 36,683
  • 19
  • 101
  • 139
Reneli
  • 1,756
  • 2
  • 16
  • 26
  • 2
    I have typically seen "(A Document Being Saved By )" when the app crashes while the app is saving. Atomic writing involves writing to a separate file before replacing the existing document -- this appears to be what this directory represents. For this reason, I suspect there is no guarantee that the data in this 'being saved' document is whole and intact, but I am also interested to see if anybody has any experience salvaging them. – Stuart Jun 04 '13 at 06:07
  • atomic writes imply no salvaging – Anton Tropashko Dec 10 '15 at 11:26

2 Answers2

6

I think Stuart's comment provides most of the answer you'll get. The fact alone that the document is called "(A Document Being Saved by )" and not "(A Document Saved by ) strongly suggests he's right.
I guess it's now up to you to figure out why saving the document crashes or never finishes or gets terminated or whatever. It could be a timeout, if your app takes too long saving the document when going to the background, a crash, or something else - I don't think you've provided enough information for anyone to give you a really precise answer.

ecotax
  • 1,933
  • 17
  • 22
2

The folder called "A Document Being Saved by AppName" inside Documents is a temporary which is used to store for the Binary large object (BLOB).

Normally, if your application is saved successfully, the folder should be emptied. However, if it crash during the save, the folder will be stored some data to let you handle it. Basically, I always remove the directories on the startup time. So, if the BLOB doesn't move into the hidden BLOB directory, it will be not in the Database.

Hope that helps.

lvarayut
  • 13,963
  • 17
  • 63
  • 87