使用networking连接一次下载一个文件
我有一个array
,其中包含不同的url。 我想用进度条下载一个文件,而不是启动下一个文件等等。
这是我迄今为止的代码;
-(void) func:(NSArray *) arry { NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration]; configuration.timeoutIntervalForRequest = 900; AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration]; NSMutableArray * downloadedUrl = [[NSMutableArray alloc] init]; for (NSString * str in arry) { NSURL *URL = [NSURL URLWithString:str]; NSURLRequest *request = [NSURLRequest requestWithURL:URL]; NSURLSessionDownloadTask downloadTask = [manager downloadTaskWithRequest:request progress:nil destination:^NSURL (NSURL targetPath, NSURLResponse response) { NSURL *tmpDirURL = [NSURL fileURLWithPath:NSTemporaryDirectory() isDirectory:YES]; return [tmpDirURL URLByAppendingPathComponent:[response suggestedFilename]]; } completionHandler:^(NSURLResponse response, NSURL filePath, NSError *error) { if(error) { NSLog(@"File Not Dowloaded %@",error); } }]; [downloadTask resume]; } }
你将如何一次下载一个文件进度条,然后从数组中删除url?
声明一个全局NSMutableArray
的文件,并在下面的函数中使用它。
-(void) downloadFile { NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration]; configuration.timeoutIntervalForRequest = 900; AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration]; NSURL *URL = [NSURL URLWithString:[self.fileArr firstObject]]; //Here self.fileArr is your global mutable array NSURLRequest *request = [NSURLRequest requestWithURL:URL]; NSURLSessionDownloadTask downloadTask = [manager downloadTaskWithRequest:request progress:nil destination:^NSURL (NSURL targetPath, NSURLResponse response) { NSURL *tmpDirURL = [NSURL fileURLWithPath:NSTemporaryDirectory() isDirectory:YES]; return [tmpDirURL URLByAppendingPathComponent:[response suggestedFilename]]; } completionHandler:^(NSURLResponse response, NSURL filePath, NSError *error) { if(error) { NSLog(@"File Not Dowloaded %@",error); [self downloadFile]; } else { [self.fileArr removeObjectAtIndex:0]; if (self.fileArr.count > 0) { [self downloadFile]; } } }]; [downloadTask resume]; }
现在调用这个函数,但是在initialize
self.fileArr之前,以及调用downloadFile
方法之前。
希望这会帮助你。
一次给队列限制一个Operation,
为此,请尝试在每个操作之间添加依赖关系,然后再对其进行排队。
如果在添加队列之前添加两个操作之间的依赖关系,比如operation1和operation2,那么操作2将不会启动,直到操作1完成或取消为止。
这样做:
NSOperationQueue *operationQueue = [[NSOperationQueue alloc] init]; // Make operation2 depend on operation1 [operation2 addDependency:operation1]; [operationQueue addOperations:@[operation1, operation2, operation3] waitUntilFinished:NO];
UPDATE
// Create a http operation NSURL *url = [NSURL URLWithString:@"http://yoururl"]; NSURLRequest *request = [NSURLRequest requestWithURL:url]; AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request]; [httpClient registerHTTPOperationClass:[AFHTTPRequestOperation class]]; [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) { // Print the response body in text NSLog(@"Response: %@", [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding]); } failure:^(AFHTTPRequestOperation *operation, NSError *error) { NSLog(@"Error: %@", error); }]; // Add the operation to a queue // It will start once added NSOperationQueue *operationQueue = [[NSOperationQueue alloc] init]; [operationQueue addOperation:operation];
希望它可以帮助你..!