Skip to content

Instantly share code, notes, and snippets.

@yas375
Created August 23, 2012 08:39
Show Gist options
  • Select an option

  • Save yas375/3434327 to your computer and use it in GitHub Desktop.

Select an option

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
@yas375
Copy link
Copy Markdown
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