App Updates, NSURL, and Documents Directory

霸气de小男生 提交于 2019-12-17 11:57:07

问题


I have an app in the app store that uses Core Data to persist much of the data. The exception is storing images. I store images in subdirectories to the Documents directory and store an NSURL reference to that image in the appropriate object attribute in the core data store.

We have noticed that, when an update makes it to the app store, those images are not found, and thus don't display, using the references stored with the previous version of the app. Now, I have a suspicion that the problem is that, since we are using development devices for testing, this issue propagates because the Directory in the Applications directory to which the dev app uses differs from the one the App store is creating/using. I have noticed differences between the App store directory for the app in Applications and the one created while debugging versions in Xcode. As such, the URL stored in core data points to the wrong applications folder. This is kinda hard to debug, as I cannot download an older app version, once the new version has been released in the store.

So I have a couple questions. Can I guarantee that the Applications subdirectory in which folks who download versions of the same app will be the same, rendering this a non-issue for non-development devices?

Should I be storing relative image url's or strings to represent the location of these resources, or should I be fine with storing what ends up being the absolute urls?

Thanks so much, Felipe


回答1:


You should use relative URLs to store references to files. The absolute URL is likely to change after an app update

Files Saved During App Updates

When a user downloads an app update, iTunes installs the update in a new app directory. It then moves the user’s data files from the old installation over to the new app directory before deleting the old installation. Files in the following directories are guaranteed to be preserved during the update process:

  • Application_Home/Documents
  • Application_Home/Library

Although files in other user directories may also be moved over, you should not rely on them being present after an update.

https://developer.apple.com/library/ios/#DOCUMENTATION/iPhone/Conceptual/iPhoneOSProgrammingGuide/PerformanceTuning/PerformanceTuning.html

Thx to the sandbox, the application home is also the user home. So it is possible to use the unix tilde which is a short hand to the user home, i.e. ~/Documents, ~/Library and so on.

Use -[NSString stringByAbbreviatingWithTildeInPath] to turn a full path into a relative ~ path. And reverse it with -[NSString stringByExpandingTildeInPath].




回答2:


I think what you're looking for is the following:

NSString *appDocumentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];


来源:https://stackoverflow.com/questions/9608971/app-updates-nsurl-and-documents-directory

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