2013-08-16 95 views
1
RemoteImageDownloader *imgView = (RemoteImageDownloader*)[cell viewWithTag:1]; 

    if (imgView == nil) 
    { 
     imgView = [[RemoteImageDownloader alloc] initWithFrame:CGRectMake(0.0, 0.0, 50.0, cell.frame.size.height)]; 
     imgView.tag = 1; 
     [cell.contentView addSubview:imgView]; 
    } 
    imgView.image = nil; 
    imgView.backgroundColor = [UIColor grayColor]; 
    imgView.opQueue = self.opQueue; 
    //[imgView performSelector:@selector(DownloadRemoteImageforURL:withCachingOption:) withObject:[_marrImgUrl objectAtIndex:indexPath.row]]; 

    if ([self checkDocDirectoryforFileName:[[_marrImgUrl objectAtIndex:indexPath.row] lastPathComponent]]) 
    { 
     [imgView setImage:[UIImage imageWithData:[self checkDocDirectoryforFileName:[[_marrImgUrl objectAtIndex:indexPath.row] lastPathComponent]]]]; 
    } 
    else 
    { 
     [imgView DownloadRemoteImageforURL:[_marrImgUrl objectAtIndex:indexPath.row] withCachingOption:NSURLRequestReloadRevalidatingCacheData isNeedtoSaveinDocumentDirectory:YES]; 
    } 

-(void)DownloadRemoteImageforURL:(NSString*)strURL withCachingOption:(NSURLRequestCachePolicy)urlCachePolicy isNeedtoSaveinDocumentDirectory:(BOOL)isNeedSave 
{ 

ImageLoader *subCategoryImgLoader = [[[ImageLoader alloc] initWithUrl:[NSURL URLWithString:strURL]] autorelease]; 

subCategoryImgLoader.target = self; 
subCategoryImgLoader.didFinishSelector = @selector(imageDownloadDidFinishwithData:andOperation:); 
subCategoryImgLoader.didFailSelector = @selector(imageDownloadfailedwithErrorDesc:andOperation:); 
[self.opQueue setMaxConcurrentOperationCount:2]; 

if ([self.opQueue operationCount] > 0) 
{ 
    NSOperation *lastOperation = [[self.opQueue operations] lastObject]; 

    [subCategoryImgLoader addDependency:lastOperation]; 
} 

[self.opQueue addOperation:subCategoryImgLoader]; 

if (_actIndicatorView) 
{ 
    [_actIndicatorView removeFromSuperview], _actIndicatorView = nil; 
} 

_actIndicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge]; 
_actIndicatorView.tag = 100; 
_actIndicatorView.center = self.center; 
[self addSubview:_actIndicatorView]; 
[_actIndicatorView startAnimating]; 
} 

在上面的代碼ImageLoaderNSOperation的子類。雖然我正在檢查操作計數,但是我正在將操作添加到該操作中,但我的操作數爲零。如果我犯了錯誤,請告訴我。我沒有得到我所做的錯誤,所以我得到了零操作計數。沒有得到操作計數的操作隊列

我創建了隊列的實例,它只創建一次,我使用的是同一個實例,而不是一次又一次地創建。添加任何操作後,它顯示它有一個操作,但是當我要添加另一個操作時,我得到零計數。

RemoteImageDownloaderUIImageView的子類。我在UIViewcontroller中創建了該實例。

希望現在能夠很容易地理解我在做什麼。

現在我評論了行[self.opQueue setMaxConcurrentOperationCount:2];。現在它正在獲得手術數量。誰能告訴我爲什麼這樣?

+0

如果新操作總是需要等待上一個操作,爲什麼要將'maxConcurrentOperationCount'設置爲2?你爲什麼不把它設置爲1? –

+0

您是否真的創建了隊列實例? – Wain

+0

我已更新我的帖子 – Exploring

回答

1

「我發短信x到我的對象的屬性y但它返回0,它不應該」

是,你沒有設置的最常見原因該屬性的值。即您的情況self.opQueuenil

編輯我們已經消除了上述問題。但是,下面的內容仍然相關

話雖如此,你也有一個競爭條件,因爲操作數可能會在你的測試中大於0並增加相關性(例如,如果操作完成)之間改變。

你或許應該做這樣的事情:

NSOperation* lastOp = [[self.opQueue operations] lastObject]; 
if (lastOp != nil) 
{ 
    [subCategoryImgLoader addDependency:lastOp]; 
} 

爲operationCount的文檔中包含

此方法返回的值反映在隊列中的對象和更改操作的瞬時數完成。因此,在您使用返回值時,實際的操作次數可能會有所不同。 因此,您應該僅將此值用於近似指導,不應將其用於對象枚舉或其他精確計算

(我的粗體)

我懷疑是發生了什麼事,當您設定的最高操作2的是,到時候你回到你的第二時間碼,真的有沒有操作留在隊列中。

+0

請檢查我的更新後的帖子。我更新了我的帖子。 – Exploring