0

我从 iOS 开始,我正在尝试编写一个带有 webview 的简单文件下载器。

我需要下载多个文件,但不能同时调用(就像我见过的例子);它应该就像一个桌面浏览器:它应该可以在浏览时随时添加新操作,而不是连续下载所有...

看了又看,发现了afnetworking,一直在尝试实现,没有好的结果……因为不知道怎么管理多个操作!

我的问题是,一旦我添加了许多操作,我该如何单独调用它们?例如,仅停止或取消其中一项。

这是代码:

...
    AFHTTPClient *httpClient;
...


- (void)DownloadFileFromUrl:(NSString *)fileURL{

        NSURL *url = [NSURL URLWithString:fileURL];
        NSURLRequest *request = [NSURLRequest requestWithURL:url];

     //I've tried to init httpClient here each time, or at didLoad with a nil baseurl

     httpClient =[[AFHTTPClient alloc]initWithBaseURL: [NSURL URLWithString:@"fileURL"]];
    [httpClient.operationQueue setMaxConcurrentOperationCount:10];

    AFDownloadRequestOperation *operation = [[AFDownloadRequestOperation alloc] initWithRequest:request
                                                                                     targetPath:targetPath
                                                                                   shouldResume:YES];


        [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {

            //COOL!

        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            //failure case


            //remove the file if saved a part of it!
            NSFileManager *fileManager = [NSFileManager defaultManager];
            [fileManager removeItemAtPath:targetPath error:&error];

            if (error) {

                //ERROR

            }

            if ([operation isCancelled]) {



            }
        }];

    [operation setProgressiveDownloadProgressBlock:^(AFDownloadRequestOperation *operation1, NSInteger bytesRead, long long totalBytesRead, long long totalBytesExpected, long long totalBytesReadForFile, long long totalBytesExpectedToReadForFile)
     {



            if (totalBytesExpectedToReadForFile > 0) {
            dispatch_async(dispatch_get_main_queue(), ^{


                //NSLog(@"Operation%i: bytesRead: %d", 1, bytesRead);
                //NSLog(@"Operation%i: totalBytesRead: %lld", 1, totalBytesRead);
                //NSLog(@"Operation%i: totalBytesExpected: %lld", 1, totalBytesExpected);
                //NSLog(@"Operation%i: totalBytesReadForFile: %lld", 1, totalBytesReadForFile);
                //NSLog(@"Operation%i: totalBytesExpectedToReadForFile: %lld", 1, totalBytesExpectedToReadForFile);



            });
        }

     }];


       [httpClient.operationQueue addOperation:operation];
        //OR ?[httpClient enqueueHTTPRequestOperation:operation];


}

当我只下载一个文件时,一切似乎都很好,因为调用 httpClient.operationQueue.operations[0]... 我得到了它的数据,但我无法从任何地方执行 [[httpClient.operationQueue.operations[ 0]]暂停];

但是,如果我尝试从另一个站点下载第二个文件(我有一个要导航的 web 视图),并将其添加到 httpClient.operationQueue,它似乎不会将它添加到同一个队列中;我的意思是,如果我用计时器打电话:

NSLog(@"Operations %d", httpClient.operationQueue.operations.count);

我没有收到“操作 2”,但是:

Operations 1
Operations 1

这很烦人,因为有时当我尝试获取 operations.count 以找到我想要的操作并对其进行处理时,我找不到它!它在“并行”队列中!

有没有办法获取和处理所有活动的操作?或者每次添加一个时将每个操作存储在某种数组上的方法?(但能够“使用它们”,而不是用于只读目的..如调用暂停、取消等)

很明显,我错过了一些重要的东西,而且我是编程的初学者,但任何帮助都将不胜感激!(请,如果你能明确一点,那就更好了。我是初学者)

非常感谢,真的。

编辑: 在做了一些研究并尝试了一些事情之后,我已经解决了将 httpclient 添加到单例中的问题,并且仅在 App 初始化时使用随机 baseurl 进行设置。然后我将每个操作添加到该 Singleton-httpClient,尽管它的 url。它似乎有效,因为现在它存储了所有连接,我可以在循环它时遇到问题访问它们,但我知道这不是好方法;它必须更容易清洁,而且我知道我错过了在某种数组中管理活动连接的方式,而不是使用带有假 baseurl 的“棘手”httpclient 进行所有操作(具有不同的 url)所以。 ..我仍在接受建议和良好做法:)。谢谢!

4

1 回答 1

3

好的,提前一些提示。我强烈建议您阅读 NSOperationQueue 和 NSOperation API 文档:

NSOperationQueue: https ://developer.apple.com/library/ios/documentation/Cocoa/Reference/NSOperationQueue_class/Reference/Reference.html#//apple_ref/doc/uid/TP40004592

NSOperation: https ://developer.apple.com/library/ios/documentation/Cocoa/Reference/NSOperation_class/Reference/Reference.html#//apple_ref/doc/uid/TP40004591

我不鼓励您尝试迭代 NSOperationQueue 的操作数组。为什么?因为它很可能会以竞争条件问题结束。

然而。我建议您采用以下方法:

1) 在您的单例类中创建一个 NSDictionary,其中: key:将表示文件名 value:与该请求关联的 AFDownloadRequestOperation 请求。

2)当您需要下载新文件时:验证是否存在于您的字典中,如果不创建新操作并将其添加到字典中。

2.1)如果它确实存在,您可能想要: 2.1.1)由于 AFDownloadRequestOperation 从 NSOperation 扩展,您可以取消它。为此,您必须先检查以下条件:isExecuting、isFinished。如果其中任何一个条件为假,您可以取消操作并将其从字典中删除。

于 2013-09-12T01:28:13.693 回答