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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T00:50:58+00:00 2026-06-08T00:50:58+00:00

Now, when my app detect that file was updated on server, it download file

  • 0

Now, when my app detect that file was updated on server, it download file and user interface stuck for downloading time. I have ASIHTTPRequest wrapper in my app, but I doesn’t know how to change my download request to asynchronous.

My code:

- (void)downloadFileIfUpdated 
{
    NSString *urlString = @"http://www.mysite.com/data.plist";  
    NSLog(@"Downloading HTTP header from: %@", urlString);  
    NSURL *url = [NSURL URLWithString:urlString];  

    NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    NSString *cachedPath = [[documentPaths lastObject] stringByAppendingPathComponent:@"data.plist"];

    NSFileManager *fileManager = [NSFileManager defaultManager];  

    BOOL downloadFromServer = NO;  

    NSString *lastModifiedString = nil;
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    [request setHTTPMethod:@"HEAD"];
    NSHTTPURLResponse *response;
    [NSURLConnection sendSynchronousRequest:request returningResponse:&response error: NULL];
    if ([response respondsToSelector:@selector(allHeaderFields)]) {
        lastModifiedString = [[response allHeaderFields] objectForKey:@"Last-Modified"];
    }

    NSDate *lastModifiedServer = nil;
    @try {
        NSDateFormatter *df = [[NSDateFormatter alloc] init];
        df.dateFormat = @"EEE',' dd MMM yyyy HH':'mm':'ss 'GMT'";
        df.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
        df.timeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
        lastModifiedServer = [df dateFromString:lastModifiedString];

    }
    @catch (NSException * e) {
    NSLog(@"Error parsing last modified date: %@ - %@", lastModifiedString, [e description]);
    }
  NSLog(@"lastModifiedServer: %@", lastModifiedServer);

    NSDate *lastModifiedLocal = nil;
    if ([fileManager fileExistsAtPath:cachedPath]) {
        NSError *error = nil;
        NSDictionary *fileAttributes = [fileManager attributesOfItemAtPath:cachedPath error:&error];
        if (error) {
            NSLog(@"Error reading file attributes for: %@ - %@", cachedPath, [error localizedDescription]);
        }
        lastModifiedLocal = [fileAttributes fileModificationDate];
         NSLog(@"lastModifiedLocal : %@", lastModifiedLocal);

        [activityIndicator stopAnimating];
    }

    // Download file from server if we don't have a local file
    if (!lastModifiedLocal) {
        downloadFromServer = YES;
    }
    // Download file from server if the server modified timestamp is later than the local modified timestamp
    if ([lastModifiedLocal laterDate:lastModifiedServer] == lastModifiedServer) {


        [activityIndicator startAnimating];


        downloadFromServer = YES;
    }

    if (downloadFromServer) {


        NSLog(@"Downloading new file from server");
        NSData *data = [NSData dataWithContentsOfURL:url];
        if (data) {
            // Save the data
            if ([data writeToFile:cachedPath atomically:YES]) {
                NSLog(@"Downloaded file saved to: %@", cachedPath);
            }

            // Set the file modification date to the timestamp from the server
            if (lastModifiedServer) {
                NSDictionary *fileAttributes = [NSDictionary dictionaryWithObject:lastModifiedServer forKey:NSFileModificationDate];
                NSError *error = nil;
                if ([fileManager setAttributes:fileAttributes ofItemAtPath:cachedPath error:&error]) {
                    NSLog(@"File modification date updated");
                    [NSThread detachNewThreadSelector:@selector(loadPList) toTarget:self withObject:nil];

                    [activityIndicator stopAnimating];



                }
                if (error) {
                NSLog(@"Error setting file attributes for: %@ - %@", cachedPath, [error localizedDescription]);
                }
            }
        }   


    }

}
  • 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-08T00:50:59+00:00Added an answer on June 8, 2026 at 12:50 am
     NSURL *url = [NSURL URLWithString:@"http://allseeing-i.com"];
     __block ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
     [request setCompletionBlock:^{
        // Use when fetching text data
        NSString *responseString = [request responseString];
    
        // Use when fetching binary data
        NSData *responseData = [request responseData];
     }];
     [request setFailedBlock:^{
        NSError *error = [request error];
     }];
     [request startAsynchronous];
    

    For more details look at http://allseeing-i.com/ASIHTTPRequest/How-to-use#using_blocks

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

Sidebar

Related Questions

Now that Google App Engine natively supports Django 1.0, I updated with the following
I have main config file /app/config.yml . Now i have services.yml in myBundle/resources/config/services.yml Do
I have an android app that is getting fairly large and complex now, and
Almost every ios app now has something like Feed option. Programming that usually includes
I had an application that hooked onto 1 single database. The app now needs
I have a Rails app installed on a Slicehost server running Apache 2 and
I have set up some gesture recognition in an app that I'm building. One
I'm developing an app that calculates poker odds, and now I'm facing some problems
I have a folder that will contain a temporary file that will be created
I have to write a C app that will act as both UDP sender

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.