The contents of the document folder - the file cannot be opened because there is no such file
Context: Xcode 8.3.2 / Swift 3
I am uploading a bunch of MP3s to a sub folder inside the Documents folder. Then I confirm success:
-
try FileManager.default.contentsOfDirectory(atPath: folderPath)
I can see my files are listed - using Xcode I am doing Window - Devices - myDevice - myApp - downloadContainer. I get a copy of the sandbox on my computer desktop. I go to the corresponding subfolder in the Documents folder and of course my MP3 files are there.
- last but not least ... in my mobile app, I simply select one of the downloaded MP3 files and hit play. He plays.
Then I restart the application. No saved song will play for the above reason: The "name_here" file cannot be opened because there is no such file. FileManager.default.contentsOfDirectory () still lists them there; when i check the sandbox they are all there.
I have no idea. Ideas, anyone?
PS: the error occurs when I
try Data.init(contentsOf: URL(fileURLWithPath: file_path_here))
I usually didn't need to load stuff into memory; AVPlayer plays them just fine from disc; however, I have to decrypt them first, so I fill "NSData" with the contents of any file. It is useless to say that nothing reaches my decoder because ... The file cannot be opened because there is no such file
Why does the same code work the first time (when I load it) but fail in a later session ???
source to share
Thanks for the tip, rmaddy.
I think Friday night I'm tired and I have to go home.
Stupidity: At the time of saving, the file path looked like
"/var/mobile/Containers/Data/Application/961ED728-311E-4BDF-A599-F79CBE59A174/Documents/NameOfMyApp/some.mp3"
What I also saved in CoreData, the full absolute path.
When the application restarted, I looked for a file in that exact path. However, the Application ID has changed and the My Documents folder now looks like
"/var/mobile/Containers/Data/Application/D07BF1A5-8865-4920-B96E-541D4251B553/Documents"
This "relativeTo" parameter in the URL () constructor exists for some reason, I suppose :-)
source to share