2

I'm writing a save game function for an iPad game, and really only need 1 savegame. However, I'm not sure if I should account for some kind of savegame corruption and use current/previous save in case one gets corrupted.

Can iOS device events,interrupt the atomic writing operation and leave my save in a corrupted state? I'm talking about stuff like device being turned off, multitasking, home button, phone calls,etc. I know that the definition of atomic means it should not, but I would like to make sure.

+(void)writeSaveGameData:(NSData*)data
{
    if(data == nil)
    {
        return;
    }
     [data writeToFile:[SaveGame savegamePath] atomically:YES];

}

+(NSData*)saveGameData
{
    return [NSData dataWithContentsOfFile:[SaveGame savegamePath]];
}
Alex Stone
  • 46,408
  • 55
  • 231
  • 407
  • @Wooble that question asks and answers whether the write either fully completes or fails without corrupting the file. This question asks under what circumstances the failure might happen. I think it's a good, novel question. – danh Jan 06 '14 at 17:31
  • 1
    There are no such circumstances, which is the answer given in the other question. – Wooble Jan 06 '14 at 17:33
  • I think you're missing the distinction between the write not completing, and the write not completing with corruption. There are no circumstances for the latter. The method returns BOOL, from the docs == YES **if the operation succeeds, otherwise NO.** – danh Jan 06 '14 at 17:39
  • Well, if the write does not complete, I will still have the old save, so it's good – Alex Stone Jan 06 '14 at 18:37

1 Answers1

2

No, it doesn't mean that it never fails, atomically means two things:

1 - that your file will saved correctly and you will have no access to it till it is completely saved, there is no way to have access to a half saved file or something like that.

2 - If there were a problem during the saving process it will not be saved at all. So, if your battery ends during the saving process, you will lose the file.

To totally reply your questions, there is no way to have a corrupted file, if the device interrupt your writing for any reason, you will lose the file. You have all or nothing!

As you didn't mention if you are saving a new file or updating an old one.. If you are updating the file, just the update will be lost, if you are writing a new file, you will lose everything.

Roberto Ferraz
  • 2,511
  • 24
  • 43
  • Your wording is misleading. You either get the new file or the original file. What you can't get is a partial new file or no file at all. Your answer implies the whole file can be lost. That isn't true. You can lose the latest update but at least the previous file will still be there. – rmaddy Jan 06 '14 at 17:46
  • In case he is saving a new one he will lose everything, if he is updating, he will lose the update and have the original file. – Roberto Ferraz Jan 06 '14 at 18:13