iOS - 如何知道NSOperationQueue何时完成一些操作?

Dab*_*rut 11 iphone download nsoperation ipad ios

我需要在我的应用程序中下载目录及其内容.所以我决定实现一个NSOperationQueue,我将NSOperation子类化为实现NSURLRequest等......

问题是我一次添加所有操作,我无法弄清楚何时下载一个目录的所有文件以更新UI并启用此特定目录.

现在我必须等待所有目录中的所有文件都被下载才能更新UI.

我已经为NSOperationQueue的operationCount和NSOperation的isFinished实现了键值观察,但我不知道目录何时包含所有文件!

你有什么主意吗 ?

非常感谢

Mat*_*uch 49

添加一个"Done" NSOperation,其中包含NSOperations一个目录的所有其他目录作为依赖项.

像这样的东西:

NSInvocationOperation *doneOp = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(done:) object:nil];

NSInvocationOperation *op1 = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(doSomething:) object:nil];
[queue addOperation:op1];
[doneOp addDependency:op1];

NSInvocationOperation *op2 = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(doSomething:) object:nil];
[queue addOperation:op2];
[doneOp addDependency:op2];

NSInvocationOperation *op3 = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(doSomething:) object:nil];
[queue addOperation:op3];
[doneOp addDependency:op3];

[queue addOperation:doneOp];
Run Code Online (Sandbox Code Playgroud)

doneOp只会在之后运行op1,op2并且op3已经完成执行.


Rad*_*dix 5

[opQueue operationCount]

希望这可以帮助