gpt4 book ai didi

objective-c - 如何使用AFNetwork下载多个文件

转载 作者:行者123 更新时间:2023-12-01 17:59:40 25 4
gpt4 key购买 nike

我想使用 AFNetwork 下载多个文件,但我不知道如何实现这个?
如您所见,我创建了一个操作数组并在其中添加了 3 个任务

NSMutableArray *operations = [NSMutableArray array];
NSArray *requestArray = @[ @"...task1.zip", @"task2.zip", @"task3.zip" ];
for (int i = 0; i < 3; i++) {
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:[requestArray objectAtIndex:i]]];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path = [[paths objectAtIndex:0] stringByAppendingPathComponent:[[requestArray objectAtIndex:i] lastPathComponent]];
operation.outputStream = [NSOutputStream outputStreamToFileAtPath:path append:NO];

[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"Successfully downloaded file to %@", path);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];

[operation setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) {
NSLog(@"Operation%i: bytesRead: %d", i, bytesRead);
NSLog(@"Operation%i: bytesRead: %lld", i, totalBytesRead);
NSLog(@"Operation%i: bytesRead: %lld", i, totalBytesExpectedToRead);
}];
[operations addObject:operation];
}

然后,我该怎么办?我做了以下事情,但没有任何 react
AFHTTPClient *requestHandler = [[AFHTTPClient alloc] init];
[requestHandler enqueueBatchOfHTTPRequestOperations:operations progressBlock:^(NSUInteger numberOfCompletedOperations, NSUInteger totalNumberOfOperations) {

} completionBlock:^(NSArray *operations) {
}];

关于这个问题的任何想法?

最佳答案

AFHTTPClient *requestHandler = [[AFHTTPClient alloc] init];应替换为 AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:@""]];因为initWithBaseURL初始化很多东西。

关于objective-c - 如何使用AFNetwork下载多个文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12198300/

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