IOS How do I know when an NSOperationQueue will finish processing multiple operations?

I need an application to download directories and their contents. So I decided to implement NSOperationQueue and I subclassed NSOperation to implement NSURLRequest, etc.

The problem is that I add all operations at once, and I cannot understand when all the files for one directory are loaded in order to update the interface and include this particular directory.

Now I need to wait for all files from all directories to be downloaded to update the user interface.

I have already implemented monitoring of key values ​​for operationCount NSOperationQueue and isFinished from NSOperation, but I do not know when the directory contains all the files in it!

Do you have any ideas?

Thank you so much

+10
ios iphone ipad download nsoperation
Apr 3 2018-12-12T00:
source share
4 answers

Add Finish NSOperation , which has all other NSOperations for the same directory as a dependency.

Something like that:

 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 work after op1 , op2 and op3 .

+47
Apr 3 2018-12-12T00:
source share
 [opQueue operationCount]

Hope this helps

+5
Feb 05 '13 at
source share

One approach would be to create a Directory type class with properties such as loadedCount (initially 0) and fileCount (initialized by any number of files in the directory) and create a dictionary matching each NSOperation with the corresponding directory before adding the operation to the queue . Inside your isFinished callback, you can pull the Directory object for this NSOperation from the dictionary and increase the value of directory.loadedCount by 1. If your directory.loadedCount == directory.fileCount, activate the update for the user interface.

0
Apr 03 2018-12-12T00:
source share

You can reorganize your code to avoid the sequence of all requests at the same time. Request only queries for one directory at a time. When operationCount reaches zero, you can be sure that all requests are completed or completed, update the user interface and write down the requests for the next directory. Continue until the directory array becomes empty.

Benefits:

  • relative simplicity
  • you do not need to query the file system just to find out what was downloaded.
  • if necessary, you can re-submit failed requests without changing any other logic.
0
Apr 3 2018-12-12T00:
source share



All Articles