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];
}

在上面的代码ImageLoader中是NSOperation. 当我检查操作计数时,虽然我正在向其中添加操作,但我得到了零。如果我犯了任何错误,请告诉我。我没有得到我所做的错误,所以我的操作计数为零。

我已经创建了队列的实例,它只创建了一次,我使用的是同一个实例,而不是一次又一次地创建。添加任何操作后,它显示它有一个操作,但是当我要添加另一个操作时,我得到的计数为零。

RemoteImageDownloader是 的子类UIImageView。我已经在UIViewcontroller.

希望现在很容易理解我实际上在做什么。

现在我评论了这一行[self.opQueue setMaxConcurrentOperationCount:2];。现在它正在获取操作计数。谁能告诉我为什么会这样?

4

1 回答 1

1

最常见的原因

“我正在向我的对象的属性 y 发送消息 x,但它返回 0,它不应该”

是您没有为属性设置值。即在你的情况下self.opQueuenil

编辑 我们已经消除了上述问题。但是,下面的内容仍然是相关的。

话虽如此,您还有一个竞争条件,因为操作计数可能会在您的测试大于 0 和添加依赖项(例如,如果操作完成)之间发生变化。

你可能应该做这样的事情:

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

operationCount 的文档包含

此方法返回的值反映队列中对象的瞬时数量,并随着操作完成而变化。因此,在您使用返回值时,实际操作次数可能会有所不同。因此,您应该仅将此值用作近似指导,而不应依赖它进行对象枚举或其他精确计算

(我的大胆)

我怀疑当您将最大操作数设置为 2 时发生的情况是,当您第二次返回代码时,队列中确实没有任何操作。

于 2013-08-16T12:59:37.240 回答