[英]AFnetworking downloading multiple files
我正在使用此代碼循環遍歷數組以下載多個文件並寫入磁盤。
-(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];
但問題是它在每個文件完成后調用成功塊(它應該),但我只需要最后一次回調來重新加載一些數據並結束進度HUD。
任何正確方向的指針都會很棒。
您可以使用AFHTTPClient進行enqueueBatchOperations ,這有一個completionBlock,在所有操作完成后調用。 應該是你正在尋找的。
也許有一天這會對某人有所幫助,但我能夠使用可能存在重大問題的解決方法,但對我的簡單用法沒問題。
我只是在處理后從同步數組中刪除了每一行,然后運行我需要的代碼。
[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"];
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.