Skip to content

Instantly share code, notes, and snippets.

@yas375
Created August 23, 2012 08:39
Show Gist options
  • Save yas375/3434327 to your computer and use it in GitHub Desktop.
Save yas375/3434327 to your computer and use it in GitHub Desktop.
Handle a few operations per one view controller
Dear Mugunth, I'm using MKNetworkKit and would like to do all network stuff in my
app as nice as possible =)
The question is what to do when our view controller needs information from several
requests (i.e. in first request we got json data with text and links to images and
also we will have a couple of requests for all images). When we leave this
viewController we would like to cancel all network operations. What is an
appropriate way to do this?
My variant you could see below. What do you think about this solution?
Thanks in advance!
@interface OperationsController : NSObject
- (void)addOperation:(MKNetworkOperation *)operation;
- (void)cancelAllOperations;
@end
#import "OperationsController.h"
@interface OperationsController () {
NSMutableDictionary *operations;
}
@end
@implementation OperationsController
- (id)init {
self = [super init];
if (self) {
operations = [NSMutableDictionary dictionary];
}
return self;
}
- (void)dealloc {
[self cancelAllOperations];
}
#pragma mark - Operation
- (void)addOperation:(MKNetworkOperation *)operation {
NSString *key = [operation uniqueIdentifier];
NSOperation *oldOperation = [operations objectForKey:key];
if (oldOperation) {
[oldOperation.dependencies makeObjectsPerformSelector:@selector(cancel)];
[oldOperation cancel];
[operations removeObjectForKey:key];
}
if (operation) {
[operations setObject:operation forKey:key];
}
}
- (void)cancelAllOperations {
for (id key in operations) {
MKNetworkOperation *operation = [operations objectForKey:key];
[operation.dependencies makeObjectsPerformSelector:@selector(cancel)];
[operation cancel];
}
[operations removeAllObjects];
}
@end
@interface SampleViewController : UIViewController
@end
#import "SampleViewController.h"
#import "OperationsController.h"
@interface SampleViewController ()
@property (nonatomic, retain) OperationsController *operationsController;
@end
@implementation SampleViewController
- (id)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if (self) {
self.operationsController = [[OperationsController alloc] init];
}
return self;
}
- (void)dealloc {
[self.operationsController cancelAllOperations];
}
#pragma mark -
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
MKNetworkOperation *firstOperation = ...
[self.operationsController addOperation:firstOperation]
MKNetworkOperation *secondOperation = ...
[self.operationsController addOperation:secondOperation]
...
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[self.operationsController cancelAllOperations];
}
@end
@MugunthKumar
Copy link

All great, except that add this logic to your super view controller.
I would recommend creating a UIViewController subclass (which I would call the super view controller) for your project and add this logic into that view controller.
This view controller should be the base class for all view controllers in your project.

Otherwise, you will have to create a OperationsController object for every view controller and managing them might become harder.

@yas375
Copy link
Author

yas375 commented Aug 23, 2012

great, thank a lot! =)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment