gpt4 book ai didi

objective-c - AFnetworking 下载多个文件

转载 作者:可可西里 更新时间:2023-11-01 04:22:59 30 4
gpt4 key购买 nike

我正在使用此代码循环遍历数组以下载多个文件并写入磁盘。

-(void)download
{
//set url paths
for (NSString *filename in syncArray)
{
NSString *urlpath = [NSString stringWithFormat:@"http://foo.bar/photos/%@", filename];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlpath]];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path = [[paths objectAtIndex:0] stringByAppendingPathComponent:filename];
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 start];

但问题是它会在每个文件完成后调用成功 block ,(它应该这样做)但我只需要最后一次回调来重新加载一些数据并结束进度 HUD。

任何指向正确方向的指示都会很棒。

最佳答案

也许有一天这会对某人有所帮助,但我能够使用可能存在重大问题但对我的简单使用来说没问题的解决方法。

我只是在处理完同步数组后删除了每一行,然后运行了我需要的代码。

 [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"Successfully downloaded file to %@", path);
[SVProgressHUD showWithStatus:@"Updating Photos"];
[syncArray removeObject:filename];
if (!syncArray || !syncArray.count)
{
NSLog(@"array empty");
[[NSNotificationCenter defaultCenter] postNotificationName:@"TestNotification" object:self];
[SVProgressHUD dismissWithSuccess:@"Photos Updated"];
}

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

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