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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T23:00:16+00:00 2026-05-26T23:00:16+00:00

UPDATE 11/18/2011 Check the accepted answer. It works and its a life saver! Hey

  • 0

UPDATE 11/18/2011
Check the accepted answer. It works and its a life saver!

Hey everyone… so Im trying to figure out how to do this. I have bounced around alot of forums to try and find my answer, but no success. Either their process it too complicated for me to understand, or is just overkill. What I am trying to do is this. I have an XML file within my app. Its a 500k xml file that i dont want the user to have to wait on when loading. SO… I put it in my app which kills the load time, and makes the app available offline. What i want to do is, when the app loads, in the background (seperate thread) download the SAME xml file which MIGHT be updated with new data. Once the xml file is complete, i want to REPLACE the xml file that was used to load the file. Any suggestions or code hints would be greatly appreciated. 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-05-26T23:00:17+00:00Added an answer on May 26, 2026 at 11:00 pm

    I ended up figuring this out. Its an old post but it appears to be getting alot of views still so I thought I’d help some people out. Keep in mind this code is NOT perfect… there are many ways to skin a cat.. and this is how I skinned mine. It works, thats all that matters!

    In a nutshell how it works is…
    The app launches > checks to see if there is a saved “date/ID”. It will be in the format of “MMDD”. If there is not a saved date/ID it will create a blank one to compare later.

    When it compares if the id doesnt match, it will download a new file.
    Todays date is 11/18/2011 so the ID would be “1118”. If you launch the app again today, the IDs will match so the app will parse the local xml file instead of downloading another one. (its a huge time saver for me, my XML file is 2-4 mbs.)

    This code will download a new xml file daily, UNLESS the app has already been ran for the day. Then it will use the local file.

    //////////      applicationDidFinishLaunching     ///////////
    lastSyncArray = [[NSMutableArray alloc] initWithCapacity:0];
    
    //check documents directory to see if you have a saved "date/ID" from earlier
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *docDir = [paths objectAtIndex:0];
    NSString *fullFileNameSavedLastSyncArray = [NSString stringWithFormat:@"%@/lastSyncArray", docDir];
    BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:fullFileNameSavedLastSyncArray];
    
    if (fileExists == YES) {
        // yes you have a saved "date/ID" from earlier
        NSArray * temp = [[NSArray alloc] init];
        temp = [NSKeyedUnarchiver unarchiveObjectWithFile:fullFileNameSavedLastSyncArray];
    
        for ( int i=0; i<[temp count]; i++ ){
    
            [lastSyncArray addObject:[temp objectAtIndex:i]];
    
        }// end for loop
    
    
    }
    else {
    
        //insert blank data for comparison later
        [lastSyncArray addObject:@""];
    }
    
    [NSThread detachNewThreadSelector:@selector(checkXMLFile) toTarget:self withObject:nil];
    
    /////////// end applicationDidFinishLaunching /////////////
    
    
    
    
    // own function in App Delegate
    -(void)checkXMLFile {
    
    
        //get current month/day
        NSDateComponents *components = [[NSCalendar currentCalendar] components:NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit fromDate:[NSDate date]];
        NSInteger day = [components day];
        NSInteger month = [components month];
        NSString * currentTimeString = [NSString stringWithFormat:@"%d%d",month,day];
    
    
        //check if file exists first
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
        NSString * fileName = [NSString stringWithFormat:@"inventory.xml"];
        NSString *xmlPath = [documentsDirectory stringByAppendingPathComponent:fileName];
        BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:xmlPath];
    
        if (fileExists == YES) {
            //yes there is a saved xml file
            NSLog(@"There is a saved file from before");
    
            //compare last sync date and current date. if they mismatch... download new file
            NSString * sToCompare = [[lastSyncArray objectAtIndex:0] description];
    
            if ([sToCompare compare:currentTimeString]==0) {
                //its a match this has been downloaded today. no need to re download.
            }
            else {
    
                //clear out old download date and save new
                [lastSyncArray removeAllObjects];
    
                //save new date         
                [lastSyncArray addObject:currentTimeString];
    
                // we are downloading a new xml file and saving it
                NSString * pdfURL2 = [NSString stringWithFormat:@"http://myfile.com/inventory.xml"];
                NSString *urlString = pdfURL2;
                NSURL *url = [NSURL URLWithString:urlString];
                NSData * xmlData = [NSData dataWithContentsOfURL:url];
    
                if (!xmlData) {
    
    
                }
                else{
                    NSLog(@"EXISTS");
                    NSLog(@"Begin to save xml");
                    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
                    NSString *documentsDirectory = [paths objectAtIndex:0];
                    NSString * fileName = [NSString stringWithFormat:@"inventory.xml"];
                    NSLog(@"File to save: %@",fileName);
                    NSString *xmlPath = [documentsDirectory stringByAppendingPathComponent:fileName];
                    [xmlData writeToFile:xmlPath atomically:YES];
                    NSLog(@"XML saved");
    
    
                }
            }
    
    
    
        }
        else {
            NSLog(@"MISSING XML");
            //no there is not a saved xml file this must be first load go ahead and download
            NSString * pdfURL2 = [NSString stringWithFormat:@"http://myfile.com/inventory.xml"];
            NSString *urlString = pdfURL2;
            NSURL *url = [NSURL URLWithString:urlString];
            NSData * xmlData = [NSData dataWithContentsOfURL:url];
    
            if (!xmlData) {
    
    
            }
            else{
                NSLog(@"EXISTS");
                NSLog(@"Begin to save xml");
                NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
                NSString *documentsDirectory = [paths objectAtIndex:0];
                NSString * fileName = [NSString stringWithFormat:@"inventory.xml"];
                NSLog(@"File to save: %@",fileName);
                NSString *xmlPath = [documentsDirectory stringByAppendingPathComponent:fileName];
                [xmlData writeToFile:xmlPath atomically:YES];
                NSLog(@"XML saved");
    
    
            }
        }
    
    
        [self sendToParser];
    
    
    }
    
    
    
    - (void)sendToParser{
    
        NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    
    
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
        NSString * fileName = [NSString stringWithFormat:@"inventory.xml"];
        NSString *xmlPath = [documentsDirectory stringByAppendingPathComponent:fileName];
    
        [self parseXMLFileAtURL:xmlPath];
    
        [pool release];
    
    }
    
    
    - (void)parseXMLFileAtURL:(NSString *)URL //URL is the file path (i.e. /Applications/MyExample.app/MyFile.xml)
    {   
        //you must then convert the path to a proper NSURL or it won't work
        NSURL *xmlURL = [NSURL fileURLWithPath:URL];
        NSData * data = [[NSData alloc] init];
        data = [NSData dataWithContentsOfURL:xmlURL];
    
        // here, for some reason you have to use NSClassFromString when trying to alloc NSXMLParser, otherwise you will get an object not found error
        // this may be necessary only for the toolchain
        NSXMLParser *parser = [[NSXMLParser alloc] initWithData:data];
    
        // Set self as the delegate of the parser so that it will receive the parser delegate methods callbacks.
        [parser setDelegate:self];
    
        // Depending on the XML document you're parsing, you may want to enable these features of NSXMLParser.
        [parser setShouldProcessNamespaces:NO];
        [parser setShouldReportNamespacePrefixes:NO];
        [parser setShouldResolveExternalEntities:NO];
    
        [parser parse];
    
        [parser release];
    }
    
    
    
    
    - (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespace qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
    {
    
        //your code here
        return;
    
    }
    
    - (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
    {
        //your code here
    }
    
    
    - (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespace qualifiedName:(NSString *)qName
    {
        //your code here
        return;
    }
    
    
    - (void)parserDidEndDocument:(NSXMLParser *)parser{
        //your code here
    
    }
    
    
    
    
    
    
    //////////   application will terminate    /////////////
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *docDir = [paths objectAtIndex:0];
    NSString *fullFileNameSavedLastSyncArray = [NSString stringWithFormat:@"%@/lastSyncArray", docDir];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    BOOL fileExists fileExists = [[NSFileManager defaultManager] fileExistsAtPath:fullFileNameSavedLastSyncArray];
    
    //check to see we have a last sync array
    fileExists = [[NSFileManager defaultManager] fileExistsAtPath:fullFileNameSavedLastSyncArray];
    if (fileExists == YES) {
        //file exists delete it so we can recreate it here in a sec
        [fileManager removeItemAtPath:fullFileNameSavedLastSyncArray error:NULL];
    }
    if ([lastSyncArray count] >=1) {
        [NSKeyedArchiver archiveRootObject:lastSyncArray toFile:fullFileNameSavedLastSyncArray];
    }
    
    
    ////// end application will terminate ///////////
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

UPDATE 2011.09.13 This bug has been resolved by Adobe. The example code below now
Update 2011-Jan-06: Believe it or not, I went ahead and incorporated this interface into
********Update********** var_dump: string(0) I am trying to check if part of an array is
I am using Visual studio 2011 beta with the april 2012 update installed .I
Update: Is there a way to achieve what I'm trying to do in an
update: I mistyped 2 variables...so embarrassing. thanks everyone for the effort! sorry i find
Update: I reported this as a bug to Apple and they fixed it! All
UPDATE: I've been playing around with this more, and it seems like tmux's clear-history
Update : This is no longer an issue from C# 6, which has introduced
I'm trying to bind a plugin to the update contact event in Microsoft Dynamics

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.