Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSData writeToFile:atomically: doesn't immediately save the file

Tags:

ios

nsdata

I'm downloading an SQLite db from the web into my iPad app. If I write it to disk setting atomically: YES I can't immediately use it because even though the file is there, sqlite complains that the tables aren't there. If I use atomically = NO or I delay the opening of the file a few instants then I don't have this problem.

I guess I could go about it by setting atomically = NO but then again is there some sort of guarantee that the whole file has been written to disk right after the writeToFile: call? So far my db is not that big but it will eventually, plus I don't know how long to wait for in other devices.

Apple docs say that this method returns YES if the operation succeedes but obviously that does not take into account the "lag" saving the file.

Any help is greatly appreciated!

EDIT: I see other people are having the same problem.

like image 354
Julian Avatar asked Jun 22 '12 17:06

Julian


1 Answers

According to the link, the operation will either write completely or fail.

With that in mind, write atomically on another thread, and then do something like this!

while (![[FileManager defaultFileManager] fileExistsAtPath:yourEventualDBPath]) {

[NSThread sleepForTimeInterval:.5];

}
like image 69
Nick Esposito Avatar answered Nov 01 '22 06:11

Nick Esposito