check if file has been updated on server

In my application and at startup I check if databases has been changed on server with the copy stored locally. I'm using a synchronous request to the server and I'm doing a check based on the last modified date time fields in the HTTP response

If the last modified data time of the file on the server is > last modified date time of the local file I ask user if he would like to update the database, if he accepts I download the database.

I'm using my machine as server but the problem when I shutdown my machine, the application crash at startup

Thanks for you help

You will find below my code

#import "FirstViewController.h"

@interface FirstViewController ()

@end

@implementation FirstViewController


- (void)viewDidLoad
{
    [super viewDidLoad];



    // check connectivity
    if ([[Reachability reachabilityForInternetConnection] currentReachabilityStatus] == NotReachable) {
        [self displayConenctivityAlert];

    }else{

        [self checkDatabases];

    }



}




- (void) checkDatabases {


    bool res = [self fileUpdated];


    if (res){
        // Ask user if he would like to update the databases    
    }

}




-(void) displayConenctivityAlert{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:[TSLanguageManager localizedString:@"NO_CONNECTED"] delegate:self cancelButtonTitle:[TSLanguageManager localizedString:@"OK"] otherButtonTitles:nil];

    [alert show];
}



- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
    NSLog(@"Error HTTP ...");
}




- (BOOL)fileUpdated {
    NSString *urlString = @"http://192.168.0.10:8888/fuel/stations.db";
    NSLog(@"Downloading HTTP header from: %@", urlString);
    NSURL *url = [NSURL URLWithString:urlString];


    //store locally data into the resource folder.
    NSString *documentsDirectory = [Utility documentsPath];



    NSString *cachedPath = [documentsDirectory stringByAppendingPathComponent:@"stations.db"];
    NSLog(@"Local URL / %@", cachedPath);
    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);
    }

    // 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) {
        downloadFromServer = YES;
    }

    return downloadFromServer;
}

@end

Answers


Your app is crashing because it is taking too long to complete didFinishLaunching the system watchdog kills your app. This is because you are making a synchronous http request in viewDidLoad of your root view controller, which must be completed before you can finish launching. You can resolve this issue multiple ways, either do your HTTP request asynchronously by calling sendAsynchronousRequest:queue:completionHandler on your NSURLConnection. The other option is to move this code out of your launching pipeline, perhaps by moving the code to viewDidAppear of your view controller. This has the side effect of performing the check on every return to the view, not just the initial load.

In short, doing a synchronous HTTP request is a big no no because your UI will hang until the request is complete. In this case it's especially bad because you're forcing your launch to be delayed until the request is complete, which is causing it to fail when the server is down.


Problem solved

I'm using now

[NSURLConnection sendAsynchronousRequest:request    queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error){


}

instead of

[NSURLConnection sendSynchronousRequest:request returningResponse:&response error: NULL];

Need Your Help

How do I Sort a Multidimensional Array in PHP

php sorting multidimensional-array

I have CSV data loaded into a multidimensional array. In this way each "row" is a record and each "column" contains the same type of data. I am using the function below to load my CSV file.

How to Return Distinct Records Based on Two Different Values

sql oracle oracle10g

Have the following sample of data in an Oracle table called MY_ATTRIBUTES: