ASIHTTPRequest自定义进度跟踪

时间:2013-03-27 20:31:30

标签: iphone ios protocols asihttprequest

我已经阅读了documentation中使用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);
}

我在这里做错了什么?

1 个答案:

答案 0 :(得分: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个小时试图解决这个问题。我希望这可以使某人更加恶化。