Created
August 23, 2012 08:39
-
-
Save yas375/3434327 to your computer and use it in GitHub Desktop.
Handle a few operations per one view controller
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@interface OperationsController : NSObject | |
- (void)addOperation:(MKNetworkOperation *)operation; | |
- (void)cancelAllOperations; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@interface SampleViewController : UIViewController | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.