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。操作数を確認しているうちに、操作を追加しているのにゼロになっています。私が間違いを犯した場合はお知らせください。私が行った間違いを取得していないため、操作カウントがゼロになっています。

キューのインスタンスを作成しましたが、それは一度だけ作成され、何度も作成する代わりに同じインスタンスを使用しています。操作を追加した後、1 つの操作があることを示していますが、別の操作を追加しようとすると、カウントがゼロになります。

RemoteImageDownloaderのサブクラスですUIImageView。でそのインスタンスを作成しましたUIViewcontroller

私が実際に何をしているのかを簡単に理解できるようになることを願っています。

今、私はその行にコメントしました[self.opQueue setMaxConcurrentOperationCount:2];。現在、操作カウントを取得しています。なぜそうなのか誰にも教えてもらえますか?

4

1 に答える 1

1

最も一般的な理由は

「メッセージ x をオブジェクトのプロパティ y に送信していますが、0 を返しますが、送信すべきではありません」

プロパティの値を設定していないということです。つまり、あなたの場合self.opQueuenilです。

編集 上記を問題として解消しました。ただし、以下は依然として関連しています

そうは言っても、操作カウントが0より大きいテストと依存関係の追加(操作が終了した場合など)の間で変化する可能性があるため、競合状態もあります。

おそらく次のようにする必要があります。

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

operationCount のドキュメントには、

このメソッドによって返される値は、キュー内のオブジェクトの瞬間的な数を反映し、操作が完了すると変化します。その結果、戻り値を使用するまでに、実際の操作数が異なる場合があります。したがって、この値はおおよその目安としてのみ使用し、オブジェクトの列挙やその他の正確な計算には使用しないでください

(太字)

最大操作数を 2 に設定すると、2 回目にコードに戻るまでに、実際にはキューに操作が残っていないのではないかと思います。

于 2013-08-16T12:59:37.240 に答える