iOS - 如何知道NSOperationQueue何时完成处理几个操作? [英] iOS - How to know when NSOperationQueue finish processing a few operations?
问题描述
我需要在我的应用程序中下载目录及其内容。所以我决定实现一个NSOperationQueue和我的子类NSOperation来实现NSURLRequest等...
I need in my application to download directories and their content. So I decided to implement a NSOperationQueue and I subclassed NSOperation to implement NSURLRequest etc...
问题是我一次添加所有的操作,我无法弄清楚当下载一个目录的所有文件,以便更新UI并启用此特定目录。
The problem is I add all the operations at once and I can't figure out when all the files for one directory are downloaded in order to update the UI and enable this specific directory.
现在我必须等待所有目录中的所有文件都被下载以更新UI。
Now I have to wait that all the files from all the directories are downloaded in order to update the UI.
我已经为NSOperationQueue的operationCount和NSOperation的isFinished实现了键值观察,但是我不知道什么时候有目录中的所有文件!
I already implemented key-value observing for the operationCount of the NSOperationQueue and the isFinished of the NSOperation but I don't know when a directory has all the files in it !
你有什么想法吗?
非常感谢
推荐答案
添加一个完成 NSOperation
其中包含所有其他 NSOperations
为一个目录作为依赖。
Add a "Done" NSOperation
which has all other NSOperations
for one directory as dependency.
如下所示:
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];
doneOp
只会在 op1
, op2
和 op3
已执行完毕。
doneOp
will only run after op1
, op2
and op3
have finished executing.
这篇关于iOS - 如何知道NSOperationQueue何时完成处理几个操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!