Alternative to subclassing CKDatabaseOperation

Despite the docs saying you should not sublcass `CKDatabaseOperation` I tried anyway and not suprising it didn't work. It causes an assertion failure.


Here's what I'm trying to do - I have a large number of records to add to a database. So I loop through my data and add a series of `CKModifyRecordsOperation` operations. Each one handles a set of records. I want to add one last operation to the database so I can run code after all of the other operations have completed. Or I might want to add a whole series of database operations to the database with my special operation added every so often to run specific code at key times as the other operations complete.


I realize that the `modifyRecordsCompletionBlock` block of the `CKModifyRecordsOperation` can be used but that block is already setup to do other things and archtiecturely it makes more sense for this special code to be added as just another database operation.


Any ideas for a proper solution? Subclassing `CKDatabaseOperation` would be ideal. I actually created my own `CKBlockDatabaseOperation` much like the standard `NSBlockOperation` class. But again, that fails since you can't subclass `CKDatabaseOperation` for some reason. My only solution so far is to use a `CKFetchRecordsOperation` on a simple known record and use its completion handler. But that's inefficient because I don't need to do a query for what I want.

Two approaches:

1) set up notifications for records being created and when the correct number of notifications come in, do your extra code.

2) create a simple counter and increment the counter in the modifyRecordsCompletionBlock (actually, count down to zero) and when the counter hits the correct number execute the extra code.

To get this level of control you need to use a different approach for starting your operations. Where normally you would add the operation to the database, instead, set the database on the operation and add it to your own queue, set maxConcurrentOperationCount 1 to run them sequentially and position your ended operation last in the array to the queue's addAllOperations method. Alternatively, run them all simultanously and make your finished operation dependent on all of them via the addDependency method on NSOperation. Another approach is to use KVO to monitor the operation count and when it reaches zero the queue is finished.

Alternative to subclassing CKDatabaseOperation
 
 
Q