2013-08-16 44 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