Ios 检查服务器上的文件是否已更新

Ios 检查服务器上的文件是否已更新,ios,crash,nsurlconnection,Ios,Crash,Nsurlconnection,在应用程序中和启动时,我检查服务器上的数据库是否已更改,副本是否存储在本地。我正在使用对服务器的同步请求,并根据HTTP响应中上次修改的日期时间字段进行检查 如果服务器上文件的上次修改数据时间>本地文件的上次修改日期时间,我会询问用户是否要更新数据库,如果他接受,我会下载数据库 我使用我的机器作为服务器,但问题是当我关闭我的机器时,应用程序在启动时崩溃 谢谢你的帮助 你可以在下面找到我的代码 #import "FirstViewController.h" @interface FirstVie

在应用程序中和启动时,我检查服务器上的数据库是否已更改,副本是否存储在本地。我正在使用对服务器的同步请求,并根据HTTP响应中上次修改的日期时间字段进行检查

如果服务器上文件的上次修改数据时间>本地文件的上次修改日期时间,我会询问用户是否要更新数据库,如果他接受,我会下载数据库

我使用我的机器作为服务器,但问题是当我关闭我的机器时,应用程序在启动时崩溃

谢谢你的帮助

你可以在下面找到我的代码

#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

你的应用程序正在崩溃,因为完成
didfishLaunching
花费的时间太长,系统监视程序会杀死你的应用程序。这是因为您正在根视图控制器的
viewDidLoad
中发出同步http请求,必须先完成该请求,然后才能完成启动。您可以通过多种方式解决此问题,或者通过在NSURLConnection上调用
sendAsynchronousRequest:queue:completionHandler
异步执行HTTP请求。另一个选项是将此代码移出启动管道,可能是通过将代码移动到视图控制器的
viewdide
。这样做的副作用是在每次返回视图时执行检查,而不仅仅是初始加载

简而言之,执行同步HTTP请求是一个很大的禁忌,因为您的UI将挂起,直到请求完成。在这种情况下,情况尤其糟糕,因为您强制将启动延迟到请求完成,这导致服务器关闭时启动失败。

问题已解决

我现在用

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


}
而不是

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

您从崩溃中得到了什么错误?com.treenityconsulting.istation未能及时启动,我认为是由于同步http请求,这需要时间,因为de服务器已关闭这是我在回答中建议的解决方案。如果我的解决方案解决了您的问题,您应该通过单击复选框来接受我的答案,而不是自己回答。很高兴您的问题得到了解决,欢迎使用SO!