NSData writeToFile: atomically: does not save the file

I download SQLite db from the Internet to my iPad app. If I write it to install atomically: YES , I cannot use it right away because, despite the fact that the file is there, sqlite complains that there are no tables there. If I use atomically = NO or I delay opening the file for several moments, then I do not have this problem.

I think I could do this by setting atomically = NO , but again there is some guarantee that the entire file was written to disk immediately after writeToFile: call? So far, my db is not so big, but in the end, plus I do not know how long to wait on other devices.

Apple docs claims that this method returns YES if the operation succeeds, but obviously does not take into account the β€œdelay” in file saving.

Any help is much appreciated!

EDIT: I see that other people have the same problem .

+6
source share
1 answer

According to the link, the operation will either be completely recorded or a failure.

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

 while (![[FileManager defaultFileManager] fileExistsAtPath:yourEventualDBPath]) { [NSThread sleepForTimeInterval:.5]; } 
+1
source

Source: https://habr.com/ru/post/918780/


All Articles