【发布时间】:2016-05-04 06:14:40
【问题描述】:
我正在处理一个非 ARC 项目,我必须在其中下载视频,下载后我将显示它们。我正在使用 NSURLSession 下载它。
问题在进度条中。当我开始下载第四个文件时,我可以通过进度条正确下载前两个或三个文件,进度条没有正确更新,但文件与最后删除的文件一起下载。
再次删除并再次下载时会崩溃
尝试在已失效的会话中创建任务
- (void)startOrPauseDownloadingSingleFile : (UIButton *)sender
{
self.part_id_Value = self.part_id;
m_Web getIconPathForAnimation:self.part_id];
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
NSURL *urlVideo=[NSURL URLWithString:self.NonInteractiveMP4URL];
NSString *strlastcomponent=[urlVideo lastPathComponent];
FileDownloadInfo *fdi = [[FileDownloadInfo alloc] initWithFileTitle:strlastcomponent andDownloadSource:[NSString stringWithFormat:@"%@",urlVideo]];
if (!fdi.isDownloading)
{
// Check if should create a new download task using a URL, or using resume data.
if (fdi.taskIdentifier == -1)
{
**Getting Crash Here**
fdi.downloadTask = [self.session downloadTaskWithURL:[NSURL URLWithString:fdi.downloadSource]];
}
else
{
fdi.downloadTask = [self.session downloadTaskWithResumeData:fdi.taskResumeData];
}
// Keep the new taskIdentifier.
fdi.taskIdentifier = fdi.downloadTask.taskIdentifier;
// Start the download.
[fdi.downloadTask resume];
// Indicate for each file that is being downloaded.
fdi.isDownloading = YES;
}
}
我的下载和更新进度条的方法 -
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite
{
double Progress = (double)totalBytesWritten / (double)totalBytesExpectedToWrite;
NSLog(@"Progressview progress : %f",Progress);
if (totalBytesExpectedToWrite == NSURLSessionTransferSizeUnknown)
{
NSLog(@"Unknown transfer size");
}
else
{
[self performSelectorOnMainThread:@selector(updateProgress:) withObject:[NSString stringWithFormat:@"%f",Progress] waitUntilDone:NO];
}
}
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location
{
NSError *error;
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *destinationFilename = downloadTask.originalRequest.URL.lastPathComponent;
NSURL *destinationURL = [self.docDirectoryURL URLByAppendingPathComponent:destinationFilename];
if ([fileManager fileExistsAtPath:[destinationURL path]])
{
[fileManager removeItemAtURL:destinationURL error:nil];
}
BOOL success = [fileManager copyItemAtURL:location
toURL:destinationURL
error:&error];
if (success)
{
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
NSLog(@"Download completed!");
NSLog(@"destination file name : %@!",destinationFilename);
NSLog(@"Part id of file name : %@!",self.part_id);
[self performSelectorOnMainThread:@selector(updateProgress:) withObject:[NSString stringWithFormat:@"1.0"] waitUntilDone:NO];
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
NSString *filenameValue=[NSString stringWithFormat:@"%@",destinationFilename];
NSString *hideExtension=[filenameValue substringToIndex:filenameValue.length-4];
NSLog(@"filenameValue == %@", filenameValue);
[self DBUpdateVideoURLStatus:self.part_id andFileName:filenameValue];
[self DBInsertDownloads:self.part_id andFileName:filenameValue];
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:NSLocalizedString(@"Success", @"Success message") message:[NSString localizedStringWithFormat:@"%@ downloaded successfully!",hideExtension] delegate:self cancelButtonTitle:NSLocalizedString(@"Ok", @"Ok action") otherButtonTitles:nil];
alert.tag = 1;
[alert show];
[alert release];
[self.session finishTasksAndInvalidate];
}];
}
else
{
NSLog(@"Unable to copy temp file. Error: %@", [error localizedDescription]);
}
}
-(void)updateProgress:(id)progress
{
float currentProgress = [progress floatValue];
[self.downloadProgress setProgress:currentProgress animated:YES];
[self.downloadProgress setNeedsDisplay];
[self.tblview reloadData];
}
【问题讨论】:
-
您使用的是单个会话对象还是多个会话对象?你为什么打电话给
finishTasksAndInvalidate?如果进度视图没有更新,您是否看到“Progressview 进度...”消息,或“未知传输大小”或什么也没有?当您说“与上次删除的文件一起下载”时,您是什么意思? -
@Rob 我一次下载一个文件。哦,所以不需要 finishTasksAndInvalidate.. 如果进度视图没有更新,但文件正在下载,则不需要。我没有收到任何消息未知传输大小。
-
您需要进行更多诊断以准确识别故障所在。
didWriteData没有被调用吗?updateProgress没有被调用吗?downloadProgress是非nil值吗?从你的问题不清楚。请编辑问题,详细说明您迄今为止所做的诊断。 -
@Rob 我很欣赏诊断,我知道这些事情当我尝试下载并删除它并再次下载尝试在已失效的会话中创建任务 *** 终止应用程序由于未捕获的异常“NSGenericException”,原因:“在已失效的会话中创建的任务”
标签: ios objective-c nsurlsessiondownloadtask