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

  • Home
  • SEARCH
  • 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 6774513
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:47:46+00:00 2026-05-26T15:47:46+00:00

I’m currently building quite a large iPhone application. Bigger than I expected anyway. But

  • 0

I’m currently building quite a large iPhone application. Bigger than I expected anyway. But that is beside the point, the overall idea of the application is to grab JSON from a web service, sort it all into custom NSObject’s that are linked together and then present.

This goes all well and good. But, because I want the user to be able to see this information on the device without an internet connection, I need to save the information that I am presenting into the Documents folder that each Application has.

I basically implemented the NSCoding protocol into all the custom NSObject’s that would need it in order to save it into a subdirectory of the Documents directory.

This is all achieved through this function here.

- (void)applicationDidEnterBackground:(UIApplication *)application
{

     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
     NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder
     NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"Data"];
     NSString *dataFileString = [dataPath stringByAppendingPathComponent:@"Company.archive"];

     if (![[NSFileManager defaultManager] fileExistsAtPath:dataFileString]) //Does directory already exist?
     {

          MACompany *company = [MACompany sharedMACompany];

          NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
          NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder
          NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"Data"];

          NSError *error;
          if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath])  //Does directory already exist?
          {
               if (![[NSFileManager defaultManager] createDirectoryAtPath:dataPath
                                       withIntermediateDirectories:NO
                                                        attributes:nil
                                                             error:&error])
               {

            NSLog(@"Create directory error: %@", error);
               }
           }

           NSString *dataFileString = [dataPath stringByAppendingPathComponent:@"Company.archive"];

           NSMutableData *data = [[NSMutableData alloc] init];
           NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];          
           [archiver encodeObject:company forKey:@"MACompany"];
           [archiver finishEncoding];

           [[NSFileManager defaultManager] createFileAtPath:dataFileString
                                            contents:data
                                          attributes:nil];

           [archiver release];
           [data release];

     } else {

      NSLog(@"File already exists, no need to recreate, not yet anyway");

     }

}

I do the following request when the user first loads the application (application didFinishLaunchingWithOptions:) and when the user opens the application after being in the background (applicationWillEnterForeground:).

 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder
NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"Data"];
NSString *dataFileString = [dataPath stringByAppendingPathComponent:@"Company.archive"];

if (![[NSFileManager defaultManager] fileExistsAtPath:dataFileString])  //Does directory already exist?
{
    NSLog(@"Create a new Company Request");

    MAWebRequests *companyReq = [[MAWebRequests alloc] init];
    [companyReq getCompanyDetails];
    [companyReq release];

} else {

    NSLog(@"Saved Company Needs to be Decoded applicationWillEnterForeground");

    MACompany *company = [MACompany sharedMACompany];

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder
    NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"Data"];

    NSString *dataFileString = [dataPath stringByAppendingPathComponent:@"Company.archive"];

    NSData *data = [[NSData alloc] initWithContentsOfFile:dataFileString];
    NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
    [data release];

    company = [unarchiver decodeObjectForKey:@"MACompany"];
    [unarchiver finishDecoding];
    [unarchiver release];        


}

Now this works all well and good and I can pull from this file also. But, I can only grab the data stored in this file when I have Xcode’s debugger attached to the application. As soon as is stopped, the data is corrupted and doesn’t include the original data.

The data is still stored there, I can see the created file, but the actual data itself that is stored within the file is wrong…

Should I not be using the above logic to save the data to the file and then pull recreate the shared object?
Has anyone else tried to do such a thing and had success?
Is there any reason as to why I’m running into this weird issue?
Has anyone else had this issue?

Any help would be greatly appreciated, have been trying all sorts of different methods to get it to work and nothing has been able to get there. All I need to be able to do is be able to store the data there permanently until I need to update it…

  • 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-26T15:47:47+00:00Added an answer on May 26, 2026 at 3:47 pm

    I have resolved this issue by saving the MACompany object well before applicationDidEnterBackground:

    Major thanks to @OleBegemann for his aid in finding where the issue nested.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I want use html5's new tag to play a wav file (currently only supported
I am doing a simple coin flipping experiment for class that involves flipping a
We're building an app, our first using Rails 3, and we're having to build

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.