我可以保存文件在数据库中的绝对路径吗? [英] Can I save the keep the absolute path of a file in database?

查看:303
本文介绍了我可以保存文件在数据库中的绝对路径吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我将一些照片保存在沙箱中,并将其绝对路径保存在数据库中。然后我构建并再次运行,我获取路径并尝试获取照片,它说该文件不存在。

In my app I save some photos in sandbox and I keep its absolute path in database. Then I build and run again, I fetch the path and try to get the photo, it says the file doesn't exist.

最后,我发现每次都我构建并运行我的应用程序,应用程序的路径是不同的。

At last, I found that every time I build and run my app, the path of app is different.

我使用 NSTemporaryDirectory()来获取临时路径,我运行两次,得到两个结果。

I use NSTemporaryDirectory() to get the temporary path, I run two times and get two results.


/ private / var / mobile / Applications / 80E09BB5-5FEB-4C27-935E -E29DE7861392 / tmp /
/ private / var / mobile / Applications / 71427100-0DBF-42F0-B6B4-F88F6417292E / tmp /

/private/var/mobile/Applications/80E09BB5-5FEB-4C27-935E-E29DE7861392/tmp/ /private/var/mobile/Applications/71427100-0DBF-42F0-B6B4-F88F6417292E/tmp/

这是正常的吗?当用户更新我的应用时,绝对路径会改变吗?

Is it normal? When user updates my app, will the absolute path change?

如果这是正常的,我应该保留数据库中的相对路径吗?是否有一些最佳实践?

If it's normal, should I just keep the relative path in database? Is there some best practice?

推荐答案

将绝对路径存储在数据库中作为对应用程序的任何更新是一个非常糟糕的主意导致应用程序存储在 / private / var / mobile / Applications 中的不同文件夹中。在应用程序更新期间,任何文件都将被迁移,但数据库中对它们的任何引用都将不再有效。

It's a very bad idea to store the absolute path in the database as any updates to the app results in the app being stored in a different folder within /private/var/mobile/Applications. During an app update, any files will be migrated across, however any references to them within the database will no longer be valid.

因此,您必须始终存储文件路径 relative 到其他逻辑目录;例如 Documents Caches 文件夹。

You must therefore always store the filepath relative to some other logical directory; for example the Documents or Caches folder.

这可能意味着您应该将此路径存储为相关目录你的类也是如此,只有当你需要使用它时才解析为绝对路径。

This probably means you should store this path as a relative directory within your classes as well, and only resolve to an absolute path when you need to use it.

最后存储临时文件引用可能是一个坏主意,因为我相信任何临时的或者,只要有感觉就可以通过O / S从文件系统中删除缓存文件。因此,您应该使用我上面提到的技术将这些文件存储在 Documents 文件夹中。

Finally storing temporary file references is probably a bad idea, as I believe any temporary or cached files can be removed from the filesystem by the O/S whenever it feels like it. Therefore you should store these files in the Documents folder, using the techniques I mentioned above.

这篇关于我可以保存文件在数据库中的绝对路径吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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