1

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];
}

そして、私は何をすべきですか?私は次のことをしますが、何も起こりません

AFHTTPClient *requestHandler = [[AFHTTPClient alloc] init];
[requestHandler enqueueBatchOfHTTPRequestOperations:operations progressBlock:^(NSUInteger numberOfCompletedOperations, NSUInteger totalNumberOfOperations) {

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

この問題についてのアイデアはありますか?

4

3 に答える 3

5

AFHTTPClient *requestHandler = [[AFHTTPClient alloc] init];多くのものを初期化するAFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:@""]]; ため 、に置き換える必要があります。initWithBaseURL

于 2012-12-17T02:33:33.543 に答える
0
[operation start];

または NSOperationQueue が探しているものです。

于 2012-09-03T20:25:23.367 に答える