NSData writeToFile:atomically:不立即保存文件 [英] NSData writeToFile:atomically: doesn't immediately save the file

查看:200
本文介绍了NSData writeToFile:atomically:不立即保存文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从网上将SQLite数据库下载到我的iPad应用程序中。如果我将它写入磁盘设置 atomically:YES 我无法立即使用它,因为即使文件存在,sqlite也会抱怨表不存在。如果我使用 atomically = NO 或者我暂时打开文件,那么我没有这个问题。

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.

我想我可以通过设置 atomically = NO 来解决这个问题,但是再次有某种保证整个 writeToFile:call之后,文件是否已写入磁盘?到目前为止,我的数据库并不是那么大,但最终还是会,我不知道在其他设备中等待多长时间。

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文档说如果操作成功,此方法返回 YES 但显然没有考虑到滞后保存文件。

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.

非常感谢任何帮助!

编辑:我看到其他人有同样的问题

I see other people are having the same problem.

推荐答案

根据链接,操作将写入完成或者失败。

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

}

这篇关于NSData writeToFile:atomically:不立即保存文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆