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屋!