我正在使用AFHTTPRequestOperation
从远程服务器下载文件现在我的问题是AFHTTPRequestOperation
开始多次下载相同的请求,所以我想阻止下载过程执行相同的请求。
所以基本上我想要的是,如果downloadFile:
函数多次使用相同的请求调用我可以阻止下载过程启动,如果任何进程已经运行相同的请求。
以下是我的代码
AFHTTPRequestOperation *operation;
-(void)downloadFile:(NSURL *)videoUrl{
NSURLRequest *request = [NSURLRequest requestWithURL:videoUrl];
operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder
NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:VIDEO_DIRECTORY_NAME];
NSString *downloadPath = [dataPath stringByAppendingPathComponent:[NSString stringWithFormat:@"TEMP_%@",[videoUrl lastPathComponent]]];
NSString *fullPath = [dataPath stringByAppendingPathComponent:[videoUrl lastPathComponent]];
// NSLog(@"Full Path For Download Video %@",fullPath);
NSError *error=[[NSError alloc]init];
if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath])
[[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:&error]; //Create folder
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:fullPath];
if (!fileExists)
{
NSLog(@"Full Path For Download Video Started%@",fullPath);
[operation setOutputStream:[NSOutputStream outputStreamToFileAtPath:downloadPath append:NO]];
[operation setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) {
// NSLog(@"bytesRead: %lu, totalBytesRead: %lld, totalBytesExpectedToRead: %lld", (unsigned long)bytesRead, totalBytesRead, totalBytesExpectedToRead);
}];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSError *error;
if (error) {
NSLog(@"ERR: %@", [error description]);
} else {
CustomAlertView *alert=[[CustomAlertView alloc]init];
NSUserDefaults *userDefaults=[NSUserDefaults standardUserDefaults];
//check for Available Space
if([ALDisk freeDiskSpaceInBytes] >= [operation.response expectedContentLength])
{
NSLog(@"Content-lent: %lld", [operation.response expectedContentLength]);
[alert ShowNotificationInParentView:nil WithTitle:NSLocalizedString(@"Video_title", nil) Message:NSLocalizedString(@"Video_downloaded_succesfully", nil) IsSuperUser:[userDefaults boolForKey:IS_SUPER_USER] TypeOfNotification:ALERT_TYPE_ERROR IsLoggedIn:YES];
[assetManager.assetManagerDelegate didAssetManagerSucceedObject:nil ErrorCode:@"" Result:YES ResponseId:VIDEO_DOWNLOAD_RESPONSE_ID];
}
else {
[alert ShowNotificationInParentView:nil WithTitle:@"Memory Full" Message:NSLocalizedString(@"video_cannot_be_downloaded", nil) IsSuperUser:[userDefaults boolForKey:IS_SUPER_USER] TypeOfNotification:ALERT_TYPE_ERROR IsLoggedIn:YES];
[assetManager.assetManagerDelegate didAssetManagerFailedResponseWithError:[NSError errorWithDomain:@"Memory Full" code:[MEMORY_OUT_OF_STORAGE intValue] userInfo:nil]];
}
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"ERR: %@", [error description]);
[assetManager.assetManagerDelegate didAssetManagerFailedResponseWithError:error];
}];
[operation start];
}
}
请指导我任何建议或解决方案。