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

為{幸葍}努か 提交于 2019-12-01 02:44:07

问题


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

}


来源:https://stackoverflow.com/questions/11161079/nsdata-writetofileatomically-doesnt-immediately-save-the-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!