This easy-to-use and lightweight component allows to organize operations in sequences, providing a single interface for progress and completion tracking. It also makes possible the cancellation of the whole compound operation through the NSProgress instance.
The following code shows how to make a three-step operation of exporting video from ALAssetsLibrary, uploading the result to the remote server and performing some necessary cleanup. It assumes that there are two classed: ExportVidoOperation
and UploadVideoOperation
, inheriting from DKOperation
.
import <DKCompoundOperation/DKCompoundOperation.h>
static NSInteger const kExportOperationProgressFraction = 30;
static NSInteger const kUploadOperationProgressFraction = 65;
static NSInteger const kCleanupOperationProgressFraction = 5;
<...>
@property (nonatomic, strong) NSOperationQueue *queue;
@property (nonatomic, weak) NSProgress *progress;
<...>
DKCompoundOperation *operation = [[DKCompoundOperation alloc] init];
[operation addOperationCreatedUsingBlock:^DKOperation *{
return [ExportVideoOperation operationWithVideoAssetURL:assetURL];
} progressFraction:kExportOperationProgressFraction];
[operation addOperationCreatedUsingBlock:^DKOperation *{
return [UploadVideoOperation operation];
} progressFraction:kUploadOperationProgressFraction];
[operation addOperationWithOperationBlock:^(DKOperation *operation) {
operation.progress.totalUnitCount = 150;
// Perform some cleanup operations
// updating operation.progress
operation.completeOperation(YES, nil);
} progressFraction:kCleanupOperationProgressFraction];
operation.completionBlock = ^(BOOL success, NSError *error) {
if (error) {
NSLog(@"Error occured: %@", error);
return;
}
self.completedLabel.hidden = NO;
};
self.progress = operation.progress;
[self.queue addCompoundOperation:operation];
You may track changes to the progress object using KVO. For more information, visit the NSProgress Class Reference and Key-Value Observing Guide.
DKCompoundOperation is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod "DKCompoundOperation"
Daniil Konoplev, [email protected]
DKCompoundOperation is available under the MIT license. See the LICENSE file for more info.