I am working on an app that saves files to the documents directory, and then saves an absolute path for each of those files in an entity using core data. The issue is that each time I rebuild and run my app on my device, the app is saved in a different generated directory, and those file paths are now incorrect. However, my documents directory is still preserved.
I have three questions about this:
If an app is updated on the store, does it then go in a different generated directory, thus invalidating any absolute paths that could be saved in the documents directory?
This is extremely unlikely but is there any way to specify in Xcode to build the app to the same directory the previous build was in?
Is there a way to specify in Xcode that you would like all documents and data wiped with each new build you load on the device?
Thanks everyone!
You said:
"I am working on an app that saves files to the documents directory, and then saves an absolute path for each of those files in an entity using core data."
Don't do that. Ever. The path to your documents directory will be different on different devices, and different if you delete your app and reinstall it. Absolute paths are pretty much guaranteed to fail.
Use paths relative to the sandbox directory in question (documents directory, temp directory, etc)
Related
I have a data file that I need to include with my app when I distribute it. When loading any files in the app, I prefix the file name with:
Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
This works great for anything I create within the app (and for reading back), like files I download in response to a user action. But I can't for the life of me figure out how to place files there when I build my app in Visual Studio.
I've tried making a "Documents" subdirectory in the special "Resources" folder, but that didn't work (I tried setting the "Build Action" to both BundleResource and Content). When I look at the folder for my app (from using the simulator) I can see that in the "Documents" folder there's all the files I downloaded, but I can't find my data file that I'm trying to bundle ahead of time. I even searched my entire hard drive on the Mac and still couldn't find said data file.
The data file isn't an image, if it matters. Just raw binary data. How do I set it up so that this file goes into the proper documents directory at compile time, so that I can read it using the SpecialFolder.MyDocuments prefix? Thanks.
You can't. You can include files in your app bundle, and then at startup copy them from the bundle into a user folder. But this won't happen automatically.
I am using Cordova fileSystem to download some mp3 Audio files thru my App. I am using cordova.file.cacheDirectory and creating a subfolder within that to store the downloaded files. However, the problem is that each time I make some changes and run the App on iPad I get a different path.
The usual pattern is
file:///var/mobile/Containers/Data/Application/13E9EA83-94A2-4950-811E-E491AC176A78/Library/Caches/MyFolder/MyFiles.mp3
However the long number (App UUID) is not consistent. It changes each time I run the App on iPad through Xcode. Because of this I am unable to access, play, delete any files downloaded during the previous session because I am saving the file names with absolute path in a separate file for quick access in the future.
After some R&D I have realized that even though UUID changes each time, it actually refers to the same location. Hence the solution is not to save absolute path and instead, save only the Path beyond "/MyFolder/" in the above example. Each time when the App is run, the Base Path, which is actually file.cordova.cacheDirectory, should be appended to each entry to obtain the absolute Path to the file.
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.
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/
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?
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.
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.
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.
You could save the file on server and store its physical path in database. I have built similar applications in past and that is how i used it. Might not be the right way but just a thought.
Is it normal?
Well, what is the meaning of temporary?
When user updates my app, will the absolute path change?
Well, what is the meaning of temporary?
If it's normal, should I just keep the relative path in database?
To do what with it?
Is there some best practice?
Don't use a temporary path for persistent storage. What about the documents folder?
I developed an Ipad app which download different kind of files, but after a week or some days the downloaded files are removed and the app that open the file can find it anymore.
Could anybody help me, Where should I save downloaded files in my app to avoid that the file came automatically deleted?. IOS usually remove downloaded files after certain time?.
I already read this apple documentation
I know that maybe is not something complicated but I can't figure out why the file is removed If anybody can help me I'll appreciate that.
Put data cache files in the /Library/Caches
directory. Examples of files you should put in this directory include
(but are not limited to) database cache files and downloadable
content, such as that used by magazine, newspaper, and map apps. Your
app should be able to gracefully handle situations where cached data
is deleted by the system to free up disk space.
Most probably you are using caches/temp directory which the system can clear contents of in case of low space. To avoid deletion use Documents Directory.
If you are already storing in documents directory, then the file can appear missing if you are storing its hardcoded path , which can change during app update. Try storing relative path and log the contents of your documents directory to see what files exist.
save the files in your local sqlite db, data core or if you can serialize them with the standard user defaults
Since ios5, the file in the NSDocument would be upload to iCloud automatically, if the file in NSDocument is too large Apple would reject the app.
I want to know what path should I store the downloaded file, and what path to store un-completed file (Would not be deleted when application terminate)?
The Caches directory is suitable for files that can be replaced if they were to disappear. In other words, if the file you download can simply be downloaded again if it were to be deleted, then the Caches directory is a good place.
If the downloaded file isn't replaceable and the downloaded file is something that the user initiates as data for the app, then the Documents directory is actually a good place and shouldn't cause rejection.