iOS - How to know when NSOperationQueue finish processing a few operations?
2014-11-02 18:02
471 查看
转自:http://stackoverflow.com/questions/9998532/ios-how-to-know-when-nsoperationqueue-finish-processing-a-few-operations
activeoldestvotes
![](http://static.adzerk.net/Advertisers/60abb4b317034aa2af0bc697e6f02963.png)
![](http://engine.adzerk.net/i.gif?e=eyJhdiI6NDE0LCJhdCI6NCwiYnQiOjAsImNtIjoyOTExLCJjaCI6MTE3OCwiY3IiOjU5MjAsImRpIjoiYzg1YzhlNzRjYjc3NDE5MGI5OGI3Y2UzZmI3OTYyYjciLCJkbSI6MSwiZmMiOjEwMTg1LCJmbCI6NzQ3MiwiaXAiOiIxMC4xNDAuODAuMTUiLCJrdyI6ImlwaG9uZSxpb3MsaXBhZCxkb3dubG9hZCxuc29wZXJhdGlvbiIsIm53IjoyMiwicGMiOjAsInByIjoxNjA0LCJydCI6MSwicmYiOiJodHRwczovL3d3dy5nb29nbGUuY29tLmhrLyIsInN0Ijo4Mjc3LCJ1ayI6InVlMS1iMDg4N2M2ZGUxYTU0ODRlOTNmNThhOTczOTIwODQ4YSIsInpuIjo0NCwidHMiOjE0MTQ5MjIzMTYxMzJ9&s=bkuow18Islg-YhQVZy8H49h4QpM)
6down votefavorite 10 | 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 iphone ios ipad download nsoperation
| ||||
a comment |
4 Answers
activeoldestvotesup vote26down voteaccepted | Add a "Done" NSOperationwhich has all other NSOperationsfor 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]; doneOpwill only run after op1, op2and op3have finished executing.
| ||||||||||||||||||||
a comment |
![](http://static.adzerk.net/Advertisers/60abb4b317034aa2af0bc697e6f02963.png)
![](http://engine.adzerk.net/i.gif?e=eyJhdiI6NDE0LCJhdCI6NCwiYnQiOjAsImNtIjoyOTExLCJjaCI6MTE3OCwiY3IiOjU5MjAsImRpIjoiYzg1YzhlNzRjYjc3NDE5MGI5OGI3Y2UzZmI3OTYyYjciLCJkbSI6MSwiZmMiOjEwMTg1LCJmbCI6NzQ3MiwiaXAiOiIxMC4xNDAuODAuMTUiLCJrdyI6ImlwaG9uZSxpb3MsaXBhZCxkb3dubG9hZCxuc29wZXJhdGlvbiIsIm53IjoyMiwicGMiOjAsInByIjoxNjA0LCJydCI6MSwicmYiOiJodHRwczovL3d3dy5nb29nbGUuY29tLmhrLyIsInN0Ijo4Mjc3LCJ1ayI6InVlMS1iMDg4N2M2ZGUxYTU0ODRlOTNmNThhOTczOTIwODQ4YSIsInpuIjo0NCwidHMiOjE0MTQ5MjIzMTYxMzJ9&s=bkuow18Islg-YhQVZy8H49h4QpM)
up vote4down vote | [opQueue operationCount] Hope this helps
| ||
add a comment |
up vote0down vote | 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.
| ||||
a comment |
up vote0down vote | You can refactor your code to avoid enqueuing all requests at once. Enqueue only requests for a single directory at a time. When operationCountreaches 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.
| ||
add a comment |
相关文章推荐
- How To Use NSOperations and NSOperationQueues
- How To Use NSOperations and NSOperationQueues
- How To Use NSOperations and NSOperationQueues
- How To Use NSOperations and NSOperationQueues
- iOS多线程编程之NSOperation和NSOperationQueue的使用
- IOS中线程的处理(GCD,NSOperation,NSOperationQueue)
- Ios多线程编程:NSOperation和NSOperationQueue
- A few things iOS developers ought to know about the ARM architecture
- NSOperation和NSOperationQueue在iOS多线程编程中的使用
- [iOS]NSOperationQueue、NSOperation、NSInvocationOperation相关
- NSOperationQueue 以及 NSOperation 在 iOS 5 下的问题
- iOS多线程编程之NSOperation和NSOperationQueue的使用
- How can selenium web driver get to know when the new window has opened and then resume its execution
- iOS多线程编程之NSOperation和NSOperationQueue的使用
- iOS多线程编程之NSOperation和NSOperationQueue的使用
- [iOS开发站在巨人肩膀上]之How to implement Queue in Objective-C
- IOS多线程编程之NSOperation和NSOperationQueue的使用
- NSOperationQueue 以及 NSOperation 在 iOS 5 下的问题
- [转] A few things iOS developers ought to know about the ARM architecture
- iOS多线程编程之NSOperation和NSOperationQueue的使用