gpt4 book ai didi

ios - 如何使用异步请求下载文件?

转载 作者:行者123 更新时间:2023-11-29 04:32:53 24 4
gpt4 key购买 nike

现在,当我的应用程序检测到服务器上的文件已更新时,它会下载文件,并且用户界面因下载时间而卡住。我的应用程序中有 ASIHTTPRequest 包装器,但我不知道如何将下载请求更改为异步。

我的代码:

- (void)downloadFileIfUpdated 
{
NSString *urlString = @"http://www.mysite.com/data.plist";
NSLog(@"Downloading HTTP header from: %@", urlString);
NSURL *url = [NSURL URLWithString:urlString];

NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *cachedPath = [[documentPaths lastObject] stringByAppendingPathComponent:@"data.plist"];

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

[activityIndicator stopAnimating];
}

// 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) {


[activityIndicator startAnimating];


downloadFromServer = YES;
}

if (downloadFromServer) {


NSLog(@"Downloading new file from server");
NSData *data = [NSData dataWithContentsOfURL:url];
if (data) {
// Save the data
if ([data writeToFile:cachedPath atomically:YES]) {
NSLog(@"Downloaded file saved to: %@", cachedPath);
}

// Set the file modification date to the timestamp from the server
if (lastModifiedServer) {
NSDictionary *fileAttributes = [NSDictionary dictionaryWithObject:lastModifiedServer forKey:NSFileModificationDate];
NSError *error = nil;
if ([fileManager setAttributes:fileAttributes ofItemAtPath:cachedPath error:&error]) {
NSLog(@"File modification date updated");
[NSThread detachNewThreadSelector:@selector(loadPList) toTarget:self withObject:nil];

[activityIndicator stopAnimating];



}
if (error) {
NSLog(@"Error setting file attributes for: %@ - %@", cachedPath, [error localizedDescription]);
}
}
}


}

}

最佳答案

 NSURL *url = [NSURL URLWithString:@"http://allseeing-i.com"];
__block ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setCompletionBlock:^{
// Use when fetching text data
NSString *responseString = [request responseString];

// Use when fetching binary data
NSData *responseData = [request responseData];
}];
[request setFailedBlock:^{
NSError *error = [request error];
}];
[request startAsynchronous];

更多详情请查看http://allseeing-i.com/ASIHTTPRequest/How-to-use#using_blocks

关于ios - 如何使用异步请求下载文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11468563/

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