2014-07-04 58 views
0

在我的應用程序中,用戶可以下載大文件(1GB +)。我有自己的進度視圖來表示下載進度。而我使用AFNetworking這樣AFNetworking +進度視圖+效果

[operation setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead){ 
       [[UIApplication sharedApplication] setIdleTimerDisabled:NO]; 
       double progress = (double)(size + totalBytesRead)/(size + totalBytesExpectedToRead); 
       [[NSNotificationCenter defaultCenter] postNotificationName:ETBDownloadManagerProgressNotification object:component userInfo:@{ ETBDownloadProgressKey : [NSNumber numberWithDouble:progress], ETBDownloadExpectedSizeKey : [NSNumber numberWithDouble:(size + totalBytesExpectedToRead)*1.0/1024.0/1024.0] }]; 
     }]; 

我有一個問題與性能,同時下載文件。同時可以只下載一個文件。時間事件探查器顯示我更新UI並經常致電drawRect:。因此,這裏是我的發展觀setProgress:

- (void)setProgress:(double)progress 
{ 
    _progress = progress; 
    if (_progress > 1) 
     _progress = 1; 
    self.textLabel.text = [NSString stringWithFormat:@"%.f%%", 100*self.progress]; 
    [self setNeedsDisplay]; 
} 

而且發展觀的drawRect:

- (void)drawRect:(CGRect)rect 
{ 
    UIBezierPath* bezierPath = [UIBezierPath bezierPath]; 
    [bezierPath addArcWithCenter:CGPointMake(rect.size.width/2, rect.size.height/2) 
          radius:rect.size.width/2 - kLineWidth/2 - kPadding 
         startAngle:0 
         endAngle:2*M_PI 
         clockwise:YES]; 

    bezierPath.lineWidth = kLineWidth; 
    [[UIColor colorWithRed:1 green:1 blue:1 alpha:.3] setStroke]; 
    [bezierPath stroke]; 

    bezierPath = [UIBezierPath bezierPath]; 
    [bezierPath addArcWithCenter:CGPointMake(rect.size.width/2, rect.size.height/2) 
          radius:(rect.size.width/2 - kLineWidth - kPadding)/2 
         startAngle:startAngle 
         endAngle:(endAngle - startAngle) * self.progress + startAngle 
         clockwise:YES]; 

    bezierPath.lineWidth = rect.size.width/2 - kLineWidth - kPadding; 
    [[UIColor colorWithRed:1 green:1 blue:1 alpha:.3] setStroke]; 
    [bezierPath stroke]; 
} 

的問題是如何避免落後?我發現的唯一方法是減少用於更新UI的postNotification的數量。但是如何?我試圖使用setDownloadProgressBlock裏面的if (i++%3 == 0)這樣的條件,但是很醜陋:) 我也試着用self.textLabel.text = ...替換成drawRect:,但它沒有任何意義。 也許可以設置下載包的大小?

回答

2

第i ++%3 == 0可能是醜陋的,但並沒有那麼糟糕,我會做更多類似

// outside of the loop 
[[UIApplication sharedApplication] setIdleTimerDisabled:NO]; 

__block double progressTmp = 0; 
[operation setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead){ 
    double progress = (double)(size + totalBytesRead)/(size + totalBytesExpectedToRead); 

    // if there was a significant change in progress ratio 
    if ((progress - progressTmp) > 0.05) { 
     [[NSNotificationCenter defaultCenter] postNotificationName:ETBDownloadManagerProgressNotification object:component userInfo:@{ ETBDownloadProgressKey : [NSNumber numberWithDouble:progress], ETBDownloadExpectedSizeKey : [NSNumber numberWithDouble:(size + totalBytesExpectedToRead)*1.0/1024.0/1024.0] }]; 
     progressTmp = progress; 
    } 
}]; 

的0.05可能要改變的東西更與您的需求。

+0

不錯,你把我的情況改成了漂亮的,謝謝 –

+0

不客氣;) –