2013-11-20 46 views
-1

在我的应用程序和启动时,我检查数据库是否已在服务器上更改,并且副本存储在本地。我正在对服务器使用同步请求,并且正在根据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 
+0

你从崩溃得到什么样的错误呢? –

+0

com.treenityconsulting.istation无法及时启动,我认为是由于同步http请求,它需要时间,因为服务器已关闭 – user2762628

回答

2

您的应用程序崩溃,因为它需要很长时间才能完成didFinishLaunching系统看门狗杀死您的应用程序。这是因为您在根视图控制器的viewDidLoad中发出了一个同步http请求,必须先完成该请求才能完成启动。您可以通过多种方式解决此问题,可以通过在您的NSURLConnection上调用sendAsynchronousRequest:queue:completionHandler来异步执行HTTP请求。另一种选择是将此代码移出启动管道,也许将代码移至视图控制器的viewDidAppear。这对每次返回视图执行检查都有副作用,而不仅仅是初始加载。

简而言之,执行同步HTTP请求是一个很大的问题,因为您的用户界面会挂起,直到请求完成。在这种情况下,它特别糟糕,因为您迫使您的启动延迟到请求完成时,这会导致服务器关闭时失败。

-1

问题解决

我使用的,而不是

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


} 

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

这是我在我的答案中建议的解决方案。如果我的解决方案解决了您的问题,您应该点击检查来接受我的答案,以表明不是自己回答。很高兴你的问题已修复,欢迎来到SO! –

相关问题