NSData writeToFile:atomically: doesn't immedia

2019-02-22 07:12发布

问题:

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.

回答1:

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];

}


标签: ios nsdata