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 6906155
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:16:24+00:00 2026-05-27T08:16:24+00:00

I built a VERY simple cache class for my iOS app yet while it

  • 0

I built a VERY simple cache class for my iOS app yet while it seems to save the data given to it it’s never able to retrieve it. My Cache system should work by creating a CachedObject that stores the Object to cache, the key, and an expiration date. It then should archive that object and save to a file named key.cache.

Cache.h

    #import <Foundation/Foundation.h>

    @interface Cache : NSObject

    +(void)setValue:(NSObject<NSCoding> *)value forKey:(NSString *)key expires:(NSDate *)expire;

    +(NSObject<NSCoding> *)getValueFromKey:(NSString *)key;

    @end

Cache.m

    #import "Cache.h"
    #import "CachedObject.h"
    #define CACHE_LOCATION @"Library/test"

    @implementation Cache

    +(void)setValue:(NSObject<NSCoding> *)value forKey:(NSString *)key expires:(NSDate *)expire
    {
        if ((value == nil) || (key == nil))
        {
            return;
        }
        CachedObject *obj = [[CachedObject alloc] initWithObject:value expires:expire forKey:key];
        NSError *error;
        if (![[NSFileManager defaultManager] createDirectoryAtPath:CACHE_LOCATION withIntermediateDirectories:YES attributes:nil error:&error])
        {
            [error release];
            return;
        }
        NSString *fileName = [[NSString alloc] initWithFormat:@"%@.cache", key];
        NSString *fileFullPath = [CACHE_LOCATION stringByAppendingPathComponent:fileName];
        [NSKeyedArchiver archiveRootObject:obj toFile:fileFullPath];
        [fileName release];
        [obj release];
    }

    +(NSObject<NSCoding> *)getValueFromKey:(NSString *)key
    {
        NSString *fileName = [[NSString alloc] initWithFormat:@"%@.cache", key];
        NSString *fileFullPath = [CACHE_LOCATION stringByAppendingPathComponent:fileName];
        CachedObject *obj = [NSKeyedUnarchiver unarchiveObjectWithFile:fileFullPath];
        NSDate *today = [NSDate date];
        if ((obj != nil) && (obj.expires != nil) && ([obj.expires compare:today] != NSOrderedAscending))
        {
            obj = nil;
        }
        [fileName release];
        if (obj != nil)
        {
            obj = [obj autorelease];
        }
        return obj;
    }

    @end

CachedObject.h

    #import <Foundation/Foundation.h>

    @interface CachedObject : NSObject<NSCoding>
    {
        NSObject<NSCoding> *_object;
        NSDate       *_expires;
        NSString     *_key;
    }

    @property (readonly) NSObject<NSCoding> *object;
    @property (readonly) NSDate       *expires;
    @property (readonly) NSString     *key;

    -(id)initWithObject:(NSObject<NSCoding> *)aObject expires:(NSDate *)aExpires forKey:(NSString *)aKey;

    @end

CachedObject.m

    #import "CachedObject.h"
    #define EXPIRE_KEY @"expires"
    #define KEY_KEY @"key"

    @implementation CachedObject

    @synthesize object = _object;
    @synthesize expires = _expires;
    @synthesize key = _key;

    -(void)encodeWithCoder:(NSCoder *)aCoder
    {
        [_object encodeWithCoder:aCoder];
        [aCoder encodeObject:_expires forKey:EXPIRE_KEY];
        [aCoder encodeObject:_key forKey:KEY_KEY];
    }

    -(id)initWithCoder:(NSCoder *)aDecoder
    {
        if (self = [super init])
        {
            _object = [_object initWithCoder:aDecoder];
            if (self.object == nil)
            {
                return nil;
            }
            _expires = [[aDecoder decodeObjectForKey:EXPIRE_KEY] retain];
            _key = [[aDecoder decodeObjectForKey:KEY_KEY] retain];
        }
        return self;
    }

    -(id)initWithObject:(NSObject<NSCoding> *)aObject expires:(NSDate *)aExpires forKey:(NSString *)aKey
    {
        if (self = [super init])
        {
            _object = aObject;
            _expires = aExpires;
            _key = aKey;
        }
        return self;
    }

    @end
  • 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-05-27T08:16:24+00:00Added an answer on May 27, 2026 at 8:16 am

    I think part of the issue might be the way you’re serializing your _object instance variable inside CachedObject.m (Perhaps any NSCoding experts can prove me wrong?)

    I think instead of:

    -(void)encodeWithCoder:(NSCoder *)aCoder
        {
            [_object encodeWithCoder:aCoder];
            [aCoder encodeObject:_expires forKey:EXPIRE_KEY];
            [aCoder encodeObject:_key forKey:KEY_KEY];
        }
    

    You want to use:

    -(void)encodeWithCoder:(NSCoder *)aCoder
        {
            [aCoder encodeObject:_object forKey:OBJECT_KEY];
            [aCoder encodeObject:_expires forKey:EXPIRE_KEY];
            [aCoder encodeObject:_key forKey:KEY_KEY];
        }
    

    Since you want to treat _object as an instance variable, similar to your _expires and _key objects.

    Side Note: When autoreleasing objects, you don’t need to do this:

    if (obj != nil)
    {
        obj = [obj autorelease];
    }
    return obj;
    

    You can just do:

    return [obj autorelease];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I built a very simple app using a PHP form with a bit of
Here's my situation; I've built a very simple web app that looks up a
I have built a variety of little scripts using Ruby's very simple Queue class,
I built a very simple tab based app with 2 tabs, each tab tied
I have two apps. One is a very simple app that I built with
I have built a very simple blog application using Ruby on Rails. New to
I built a (very simple) library, which I can call just fine from another
I have a very simple question. Is there a built in method to shorten
This seems like a very simple problem, but I cannot get a scrollbox to
I have a very simple chat system I've built using PHP and MySQL (this

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.