2013-11-20 48 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! –

相關問題