Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7853351
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T19:39:35+00:00 2026-06-02T19:39:35+00:00

I have an in-memory cache that I would like to write out to file

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T19:39:37+00:00Added an answer on June 2, 2026 at 7:39 pm

    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];
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Ignoring unsafe code, .NET cannot have memory leaks. I've read this endlessly from many
I have a memory block that is divided into a series of location that
that's what I have: a Windows Service C# multithreaded the service uses a Read-Write-Lock
I have a thread that needs to write data from an in-memory buffer to
Is there any library for distributed in-memory cache, distributed tasks, publish/subscribe messaging? I have
Two Windows processes have memory mapped the same shared file. If the file consists
I have vague memory of a screenshot of a VS plugin that colors different
I have the below code that links and runs fine in 32bit mode -
I would like to know if there is a way to have the .NET
I have an in-memory cache which stores a set of information by a certain

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.