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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T13:07:05+00:00 2026-06-10T13:07:05+00:00

I have a singleton class that will initialize it’s data from a web service

  • 0

I have a singleton class that will initialize it’s data from a web service then save itself to a file (using NSCoding and NSKeyedUnarchiver) so I can just initialize this file instead of pulling all the data from the web again. Because it’s a singleton instance that is being saved, I want to be able to call the singleton getter like you normally would, check to see if there’s an archived copy and if not, pull the data down and archive it. Archiving is working fine, but when I try to call [[NSKeyedUnarchiver unarchiveObjectWithFile: filePath] retain] it calls the sharedInstance getter before sharedInstance is initialized. This causes init to be called and the app then downloads all the data again, just to be subsequently overwritten by the unarchiver.

Am I doing something wrong with my setup, or is there another way of Serializing this data?

Here’s some (simplified) code:

@implementation Helmets
@synthesize helmetList, helmetListVersion;
//Class Fields
static Helmets *sharedInstance = nil;

// Get the shared instance and create it if necessary.
+ (Helmets *)sharedInstance {
    //if(sharedInstance == nil){
        //[Helmets unarchive];             //This does not work! Calls sharedInstance() again (recursion)
        if(sharedInstance == nil){
            sharedInstance = [[super allocWithZone:NULL] init];    //Pull from web service
            [Helmets archive];    //Save instance
        //}
    //}
    return sharedInstance;
}

- (id)init {
    self = [super init];
    if (self) {
            helmetList = [[NSMutableArray alloc]init];

            //Get our data from the web service and save it (excluded)
            }
    }
    return self;
}

//This works!
+(void)archive{
    if([NSKeyedArchiver archiveRootObject:sharedInstance toFile:[Helmets getFilePath]]){
        NSLog(@"Archiving Successful");
    }
    else{
        NSLog(@"Archiving Failed");
    }
}

//This works, but calls getInstance causing data to be downloaded anyways!
+(void)unarchive{
    // Check if the file already exists
    NSFileManager *filemgr = [NSFileManager defaultManager];
    NSString *filePath = [Helmets getFilePath];
    if ([filemgr fileExistsAtPath: filePath])
    {
       sharedInstance = [[NSKeyedUnarchiver unarchiveObjectWithFile: filePath] retain];
    }
    [filemgr release];
}

Instance is initialized like:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
    ...
    [Helmets unarchive];        //This calls sharedInstance() too soon!
    [Helmets sharedInstance];
}

The class implements NSCoding in the .h and overrides initWithCoder and encodeWithCoder (Archiving is working).

Thanks in advance!

  • 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-10T13:07:07+00:00Added an answer on June 10, 2026 at 1:07 pm

    In the end you need a private method to set your shared instance in addition to the one you have, and you need a different init, again private to the implementation.

    - (id)initAndFetch:(BOOL)fetch
    {
        if((self = [super init])) {
            ...
    
            if(fetch) { do the web fetch };
                 ...   
        }
    }
    

    In the +sharedInstance method, you will pass YES.

    Then your decode will look like:

    - (id)initWithCoder:(NSCoder *)decoder
    {
        if((self = [self initAndFetch:NO])) {
            title = [decoder decodeObjectForKey:@"title"];
            ...
            sharedInstance = self;
        }
        return self;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have singleton class that acts as a data cache. Multiple threads will
So I have this service class that utilize Restkit to consume REST web services.
I have a class that is a singleton. This singleton is instantiated using GCD
I have an object of some class that obeys the singleton pattern. I need
I have a Singleton that is accessed in my class via a static property
I have created a singleton class to keep track of my data on my
Say that I have a singleton class (Downloader) responsible for downloading and persisting files.
I have a singleton class, and I will compile it as a library static(lib)
I have a class that is managed by ninject as a singleton that looks
I want to have a Singleton that will be auto instantiated on program start.

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.