Steven
Steven

Reputation: 1098

Writing/reading NSData to file failing

I have an in-memory cache that I would like to write out to file on viewWillDisappear and read in back into memory on viewDidLoad. My code:

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSFileManager *fileManager = [[NSFileManager alloc] init];
    NSArray *fileArray = [fileManager URLsForDirectory:NSCachesDirectory inDomains:NSUserDomainMask];
    NSString *filePath = [NSString stringWithFormat:@"%@cache_%d", [fileArray lastObject], self.index];
    NSURL *fileUrl = [NSURL URLWithString:filePath];
    if ([fileManager fileExistsAtPath:filePath]) {
        self.thumbnailsCache = [NSDictionary dictionaryWithContentsOfURL:fileUrl];
    }
}

- (void)viewWillDisappear:(BOOL)animated
{
    NSFileManager *fileManager = [[NSFileManager alloc] init];
    NSArray *fileArray = [fileManager URLsForDirectory:NSCachesDirectory inDomains:NSUserDomainMask];
    NSString *filePath = [NSString stringWithFormat:@"%@cache_%d", [fileArray lastObject], self.index];
    NSURL *fileUrl = [NSURL URLWithString:filePath];
    [self.thumbnailsCache writeToURL:fileUrl atomically:YES];
}

Based on some NSLog and debugging, it seems to write the file successfully, but on trying to read it simply says file not found. What am I doing wrong? Thanks.

Edit: self.thumbnailsCache is an NSDictionary of NSData objects.

Upvotes: 1

Views: 2532

Answers (1)

Michael Dautermann
Michael Dautermann

Reputation: 89509

You're creating your filePath incorrectly.

fileArray is an array of URL's and not NSStrings (which is what your code is assuming).

So if you're taking the last URL as being the cache directory you want to use, you can create the cache file via something like this:

NSURL * cacheURL = (NSURL *)[fileArray lastObject];
if(cacheURL)
{
    NSURL * fileToWrite = [cacheURL URLByAppendingPathComponent: [NSString stringWithFormat:@"%@cache_%d", self.index]];
}

Or, in the context of your code:

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSFileManager *fileManager = [[NSFileManager alloc] init];
    NSArray *fileArray = [fileManager URLsForDirectory:NSCachesDirectory inDomains:NSUserDomainMask];
    NSString *filePath = [NSString stringWithFormat:@"%@cache_%d", [fileArray lastObject], self.index];

    NSURL * cacheURL = (NSURL *)[fileArray lastObject];
    if(cacheURL)
    {
        NSURL * fileToRead = [cacheURL URLByAppendingPathComponent: [NSString stringWithFormat:@"%@cache_%d", self.index]];
        if(fileToRead && ([fileManager fileExistsAtPath:fileToRead]) {
                self.thumbnailsCache = [NSDictionary dictionaryWithContentsOfURL:fileToRead];
        }
    }
}

- (void)viewWillDisappear:(BOOL)animated
{
    NSFileManager *fileManager = [[NSFileManager alloc] init];
    NSArray *fileArray = [fileManager URLsForDirectory:NSCachesDirectory inDomains:NSUserDomainMask];
    NSString *filePath = [NSString stringWithFormat:@"%@cache_%d", [fileArray lastObject], self.index];

    NSURL * cacheURL = (NSURL *)[fileArray lastObject];
    if(cacheURL)
    {
        NSURL * fileToWrite = [cacheURL URLByAppendingPathComponent: [NSString stringWithFormat: @"%@cache_%d", self.index]];
        if(fileToWrite)
        {
            [self.thumbnailsCache writeToURL:fileToWrite atomically:YES];
        }
    }
}

Upvotes: 3

Related Questions