iOS - How to know when NSOperationQueue finish processing a few operations?

转自:http://stackoverflow.com/questions/9998532/ios-how-to-know-when-nsoperationqueue-finish-processing-a-few-operations


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...

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.

Now I have to wait that all the files from all the directories are downloaded in order to update the UI.

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 !

Do you have any idea ?

Thanks a lot

share | improve this question
 
 
It is more convenient to use dispatch_group_async. See this link [link][1] [1]:stackoverflow.com/questions/9632235/… –   Igor Fedorchuk  Mar 18 at 8:43

4 Answers

up vote 26 down vote accepted

Add a "Done" NSOperation which has all other NSOperations for one directory as dependency.

Something like this:

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 will only run after op1op2 and op3 have finished executing.

share | improve this answer
 
 
I forgot to mention that my operations are concurrent. Is your example still ok ? –   Dabrut  Apr 3 '12 at 21:37 
 
Of course, that's the whole point of dependencies. In a non-concurrent operation queue you would just add your operations in the correct order to achieve the same. But an operation will not run until all its dependencies have finished executing. –   Matthias Bauch  Apr 4 '12 at 10:08 
 
Kudos to you. That's exactly what I was looking for ! –   Dabrut  Apr 4 '12 at 20:14
 
Quite an elegant solution! –   Costique  Apr 5 '12 at 5:25
 
Many thanks for your answer help me –   wod  Jan 21 '13 at 7:12
[opQueue operationCount]

Hope this helps

share | improve this answer
 

One approach would be to create some sort of Directory class with a properties such as loadedCount (initially 0) and fileCount (initialized to however many files are in the directory) and create a dictionary mapping each NSOperation to the appropriate Directory before adding the operation to the queue. Inside your callback for isFinished, you could pull the Directory object for the given NSOperation out of the dictionary and increment the directory.loadedCount by 1. If your directory.loadedCount == directory.fileCount, trigger an update to the UI.

share | improve this answer
 
 
That was my first idea. But I was wondering if that was the correct way to do it or if there was a better way to do it. –   Dabrut  Apr 3 '12 at 19:08

You can refactor your code to avoid enqueuing all requests at once. Enqueue only requests for a single directory at a time. When operationCount reaches zero, you can be sure that all the requests either completed or failed, update the UI and enqueue the requests for the next directory. Proceed until the array of directories is empty.

The advantages are:

  • relative simplicity
  • you don't have to query the file system only to figure out what has been downloaded
  • if need be, you can re-enqueue failed requests without changing other logic.
share | improve this answer
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值