私はtableView
と40の細胞を持っています。ユーザーがセルをクリックすると、セルがチェックされます(ファイルが存在するかどうか)。ファイルが存在しない場合、ダウンロードが開始されます。Objective-C:40ファイルをダウンロード
質問:
ユーザーが、私はこのコードを使用している場合私のアプリが失敗したり、遅れる一度にすべての40個のファイルをダウンロードしたい場合は?あなたが問題を引き起こして40の同時ダウンロードに問題があってはなりません
- (void)viewDidLoad
{
[super viewDidLoad];
_session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration] delegate:self delegateQueue:[NSOperationQueue mainQueue]];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row == 1) {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"1.mp3"];
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:filePath isDirectory:false];
if (!fileExists) {
UIAlertController * alert = [UIAlertController
alertControllerWithTitle:@"1"
message:@"1"
preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction* actionAdd = [UIAlertAction
actionWithTitle:@"OK"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
[alert dismissViewControllerAnimated:YES completion:nil];
_url1 =[NSURL URLWithString:@"link"];
_downloadTask1 = [_session downloadTaskWithURL:_url1];
[_downloadTask1 resume];
self.circularPV1 = [[FFCircularProgressView alloc] initWithFrame:CGRectMake(0, 0, 24, 24)];
[tableView cellForRowAtIndexPath:indexPath].accessoryView = self.circularPV1;
[self.view addSubview:_circularPV1];
[_circularPV1 startSpinProgressBackgroundLayer];
}];
UIAlertAction* actionCancel = [UIAlertAction
actionWithTitle:@"Cancel"
style:UIAlertActionStyleCancel
handler:^(UIAlertAction * action)
{
[alert dismissViewControllerAnimated:YES completion:nil];
}];
[alert addAction:actionAdd];
[alert addAction :actionCancel];
alert.popoverPresentationController.sourceView = self.view;
alert.popoverPresentationController.sourceRect = CGRectMake(self.view.bounds.size.width/3.4, self.view.bounds.size.height/4.0, 1.0, 1.0);
[self presentViewController:alert animated:YES completion:nil];
}
if (fileExists) {
[self performSegueWithIdentifier: @"Segue" sender: self];
}
}
}
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location
{
if (downloadTask == _downloadTask1) {
_paths1 = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
_documentsDirectory1 = [_paths1 objectAtIndex:0];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSURL *newLocation = [NSURL URLWithString:[NSString stringWithFormat:@"file://%@/1.mp3", _documentsDirectory1]];
NSError *error;
[fileManager copyItemAtURL:location toURL:newLocation error:&error];
}
}
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite;
{
if(downloadTask == _downloadTask1){
self.p1 = (float) totalBytesWritten;
self.p2 = (float) totalBytesExpectedToWrite;
[_circularPV1 setProgress:(p2/p2)];
[_circularPV1 stopSpinProgressBackgroundLayer];
}
ファイルを読み書きするために別のスレッドを使用してください。 –
私は 'dispatch_async(dispatch_get_main_queue()、^ { });'を追加する必要があります。 – user
ファイルをダウンロードするには、独自の(バックグラウンド)操作キューと 'NS(ブロック)操作 'を使用します。例えば、[this Apple sample code](https://developer.apple.com/library/content/samplecode/LazyTableImages/Introduction/Intro.html)を見てください。 – vadian