1

what happens in iOS when nscoding writeToFile is saving and user closes the app?

    NSData *d = [NSKeyedArchiver archivedDataWithRootObject:self];
    [d writeToFile:FILE_PATH atomically:YES];

For example suppose there is 100 MB of user data to save - it will probably take a while (NSData writeToFile is really slow) - so if the user closes the app, will the write process just not finish and you wouldn't have your data synced?

If so, is there a way to prevent the user from closing the app until you are done saving?

Burhanuddin Sunelwala
  • 5,318
  • 3
  • 25
  • 51
user1709076
  • 2,538
  • 9
  • 38
  • 59
  • The app become a background app when you press the home button. It doesn't close. The OS can hibernate background apps on low memory. – Tien Dinh Apr 10 '15 at 14:45

1 Answers1

2

Atomically, to put simply, makes ensures either ALL of the file is written or NONE of it.

Nonatomically allows some of the file to be written and cut off.

If there is some reason why the app is terminated before the whole file is written and you're using atomically, the file will NOT be written to storage. The process will be killed.

If the file is that large, I'd recommend starting up some sort of loading screen that tells the user not to close the app, and after it's completion hiding that loading screen to let them know that it's alright to close the app.

AlexKoren
  • 1,605
  • 16
  • 28
  • Is that a reason why people use core data? Because the data has a higher probability of being saved locally if the user were to try and close the app? – user1709076 Apr 12 '15 at 14:30
  • People use Core Data because it works through app updates and versions (and for other several other reasons). It's a more robust system, but is more complicated to implement. – AlexKoren Apr 12 '15 at 18:26