Document or Cache Path Changes on Every Launch in iOS 8

Document Or cache Path Changes on every launch in iOS 8

I got the answer. As the absolute path is changing on every launch, we can save the data on relative path and retrieve it on appending absolute path and relative path.

This is how we can save the data on the relative path:

NSString *documentsDirectory = @"MyFolder";
documentsDirectory = [documentsDirectory stringByAppendingPathComponent:filename];
NSString *relativePath = documentsDirectory;

But when you read the file you have to use absolute path + relative path:

  NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *fullCachePath = ((NSURL*)[[fileManager URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject] ).path;
NSString *fullPath = [fullCachePath stringByAppendingPathComponent:relativePath];

For Database also store data on the relative path only. But while reading take the absolute path and append the relative path coming from database and read.

Its Working here.

Document directory path change when rebuild application

iOS 8 onwards, Absolute url to app's sandbox changes every time you relaunch the app. Hence you should never save the absolute url of the video. Save the name of the video and recreate the url every time you relaunch the app.

  let pathComponent = "pack\(self.packID)-\(selectRow + 1).mp4"
let directoryURL: URL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
let folderPath: URL = directoryURL.appendingPathComponent("Downloads", isDirectory: true)
let fileURL: URL = folderPath.appendingPathComponent(pathComponent)

Once you have fileURL look for the file and you will find the file downloaded in previous launch.

iOS creates a new Sandbox for app every time user launches the app. Hence absolute URL will very. But iOS will take care of setting up all the folders and contents inside the Sandbox as it was earlier. So though base url of SandBox change, relative url's of all the content will be remained intact.

Hence its advised never to save absolute url to any folder :) Hope it helps

Library/Caches path not found in iOS 8

At last I found the answer. This work in iOS 6,7 as well as 8. Just need to change the below line.

From

NSString *resoursePath=[[NSString alloc] initWithString:[[[[NSBundle mainBundle] resourcePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"Library/Caches"]];

To

NSString *resoursePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];

Handling ios Bundle Path Changes

I will take this approach as it is the one used in the Big Nerd Ranch book:

https://github.com/ninjinkun/ios-programming-the-big-nerd-ranch-guide/blob/master/Homepwner/Homepwner/Model/ImageStore.m

http://www.bignerdranch.com/

Document directory path of Xcode Device Simulator

on my computer, the path is:

~/Library/Developer/CoreSimulator/Devices/1A8DF360-B0A6-4815-95F3-68A6AB0BCC78/data/Container/Data/Application/

NOTE: probably those long IDs (i.e UDIDs) are different on your computer.

Xcode 6 keeps renaming my app's directory in iOS8 simulator after each run.

Turns out Xcode 6 does in fact change the app's UUID every run, and I'm in the wrong for storing absolute paths.

Saved file is gone after recompiling the App

The Document Directory path is different after each run.

Save to NSUserDefaults only the relative path from the Documents Directory (what you append to the getDocumentsURL() NSURL).

So in your case, you don't specify a subdirectory in your apps Document Directory, so the only thing you need on the next run, is the file name that you saved.

After that, you can retrieve the saved image with:

 func loadImageNamed(name: String) -> UIImage? {

let imagePath = fileInDocumentsDirectory(name)
let image = UIImage(contentsOfFile: imagePath)

if image == nil {

print("missing image at: \(path)")
}
print("Loading image from path: \(path)") // this is just for you to see the path in case you want to go to the directory, using Finder.
return image

}

Can't find saved file (in device) after restarting the app

You shouldn't store raw file paths for persistence (or if you do, know that the root can move on you). A better practice would be to only store the relative part of the path and always attach it to the current "root" path in question (particularly if you might be sharing data across devices as with iCloud).

Still, it is common to take the shortcut, so here's some quick-and-dirty code to take a file path (assumed to be in the Documents directory, you can tweak to taste if you're using a different location) and update it to the current Documents path.

+ (NSString *)rebasePathToCurrentDocumentPath:(NSString *)currentFilePath 
{
NSString *fileComponent = [currentFilePath lastPathComponent];
NSArray *currentDocumentDir = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
NSString *currentDocumentPath = [currentDocumentDir objectAtIndex: 0];
NSString *rebasedFilePath = [currentDocumentPath stringByAppendingPathComponent:fileComponent];
return rebasedFilePath;
}

Attach this to a utils class or otherwise integrate it into your flow..

iOS: Documents directory being 'cleaned'

The documents directory is the recommended place to store a core data database and iOS will never "clean up" anything stored there.

Users can manually delete files in the Documents directory, by uninstalling the app or (if you've enabled it in info.plist) browsing their phone from in iTunes.

Most users do not expect their data to be destroyed when they uninstall an app (Macs and PCs would leave the data in place for example), so this is probably what's happening.

You should consider storing a second copy of the data on your server, or on the user's iCloud account. That way it won't be destroyed by an uninstall. If it's your server, then you can justify charging money for this feature (recurring revenue is good right?).

Backups to iTunes and iCloud will both include your database, so you can instruct users to restore to a recent backup to get their data back.

Also double check your code to see how it handles an out of disk space error when attempting to save changes to the database. Depending how you're using Core Data, this could go bad.

These days Core Data in iCloud or some other cloud solution is the best approach.



Related Topics



Leave a reply



Submit