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 ()


@implementation FirstViewController

- (void)viewDidLoad
    [super viewDidLoad];

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


        [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 = @"";
    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;



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.

Need Your Help

List items appearing below other list items containing images

html css alignment

I'm trying to create a simple navigation that consists of five list items. All of which are 20% in width. There are two items containing text, a centered item containing the site logo as an image and

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.