gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 17:45:45 25 4
gpt4 key购买 nike

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

最佳答案

您的应用崩溃是因为完成didFinishLaunching花费的时间太长,系统监视程序杀死了您的应用。这是因为您正在用根 View Controller 的viewDidLoad发出同步http请求,该请求必须先完成,然后才能完成启动。您可以通过多种方式解决此问题,或者通过在NSURLConnection上调用sendAsynchronousRequest:queue:completionHandler异步执行HTTP请求。另一个选择是将此代码移出启动管道,也许是通过将代码移至 View Controller 的viewDidAppear来实现。这具有对 View 的每次返回执行检查的副作用,而不仅仅是初始加载。

简而言之,执行同步HTTP请求非常重要,因为您的UI会在请求完成之前挂起。在这种情况下,这特别糟糕,因为您将强制启动推迟到请求完成之前,这会导致服务器关闭时失败。

关于ios - 检查服务器上的文件是否已更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20107062/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com