[英]Not getting Operation Count for Operation Queue
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];
}
在上面的代碼中, ImageLoader
是NSOperation
的子類。 當我檢查操作計數時,盡管我要向其中添加操作,但得到的卻是零。 如果我有任何錯誤,請告訴我。 我沒有犯什么錯誤,所以我的操作計數為零。
我已經創建了隊列的實例,並且只創建了一次,並且我使用的是同一實例,而不是一次又一次地創建。 添加任何操作后,它表明它具有一個操作,但是當我要添加另一個操作時,我得到的計數為零。
RemoteImageDownloader
是UIImageView
的子類。 我已經在UIViewcontroller
創建了該實例。
希望現在可以很容易理解我的實際工作。
現在我注釋了這一行[self.opQueue setMaxConcurrentOperationCount:2];
。 現在,它正在獲取操作計數。 誰能告訴我為什么?
最常見的原因
“我正在將消息x發送到對象的屬性y,但它返回0,但不應返回”
是您尚未為屬性設置值。 即在您的情況下self.opQueue
為nil
。
編輯 我們已經消除了上述問題。 但是,以下內容仍然有意義 。
話雖如此,您也有一個競爭條件,因為操作計數可能會在測試大於0並添加依賴項(例如,如果操作完成)之間發生變化。
您可能應該執行以下操作:
NSOperation* lastOp = [[self.opQueue operations] lastObject];
if (lastOp != nil)
{
[subCategoryImgLoader addDependency:lastOp];
}
operationCount的文檔包含
此方法返回的值反映了隊列中對象的瞬時數量,並且隨着操作完成而發生變化。 因此,到您使用返回值時,實際的操作數可能會有所不同。 因此,您應該僅將此值用於大概指導,而不應將其用於對象枚舉或其他精確計算 。
(我的粗體)
我懷疑將最大操作數設置為2時發生的情況是,到第二次返回代碼時,隊列上確實沒有剩余操作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.