0

我已经阅读了有关在文档中使用 ASIHTTPRequest 在我的应用程序中实现自定义进度跟踪的信息。它提到了实现 2 种方法,request:didReceiveBytes:并且request:incrementDownloadSizeBy:. 我已将这些添加到我的视图控制器中,但它们没有被调用。我只是想让示例代码在这一点上工作。

// ViewController.m

- (void)fetchThisURLFiveTimes:(NSURL *)url
{
    [myQueue cancelAllOperations];
    [myQueue setDownloadProgressDelegate:self];
    [myQueue setDelegate:self];
    [myQueue setRequestDidFinishSelector:@selector(queueComplete:)];
    int i;
    for (i=0; i<5; i++) {
        ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
        request.showAccurateProgress = YES;
        [myQueue addOperation:request];
    }
    myQueue.showAccurateProgress = YES;
    [myQueue go];
}

- (void)queueComplete:(ASINetworkQueue *)queue
{
   NSLog(@"Value: %f", [myProgressIndicator progress]);
}

- (void)request:(ASIHTTPRequest *)request didReceiveBytes:(long long)bytes
{
    NSLog(@"bytes: %lld", bytes);
}

- (void)request:(ASIHTTPRequest *)request incrementDownloadSizeBy:(long long)newLength
{
    NSLog(@"newLength: %lld", newLength);
}

我在这里做错了什么?

4

1 回答 1

1

哇。所以你把线放在哪里self.myQueue.showAccurateProgress = YES;真的很重要。它必须在该行之前添加[self.myQueue addOperation:request];。这对我有用。

- (void)fetchThisURLFiveTimes:(NSURL *)url
{
    [self.myQueue cancelAllOperations];        
    [self setMyQueue:[ASINetworkQueue queue]];
    [self.myQueue setDownloadProgressDelegate:self];
    [self.myQueue setDelegate:self];
    [self.myQueue setQueueDidFinishSelector:@selector(queueFinished:)];
    self.myQueue.showAccurateProgress = YES;

    int i;
    for (i=0; i<5; i++) {
        ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
        [self.myQueue addOperation:request];
    }
    [self.myQueue go];
}

- (void)queueFinished:(ASINetworkQueue *)queue
{
    if ([self.myQueue requestsCount] == 0) {
        [self setMyQueue:nil];
    }
    NSLog(@"Queue finished");
}

- (void)setProgress:(float)newProgress {
    NSLog(@"progress: %f", newProgress);
}

试图弄清楚这一点真是浪费了 6 个小时。我希望这可以减轻一些人的麻烦。

于 2013-03-28T16:46:12.603 回答