Created
October 18, 2013 14:57
-
-
Save mteece/7042809 to your computer and use it in GitHub Desktop.
Objective-C blocks and Grand Central Dispatch. Using GCD and blocks effectively.
This file contains hidden or 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 <Foundation/Foundation.h> | |
@interface EmailRepository : NSObject | |
+ (id)sharedEmailRepository; | |
- (void)getDataFromServer:(NSString *)emailAddress onComplete:(void (^)(NSString *response))completionBlock onFailure:(void (^)(NSString *response))failureBlock; | |
@end |
This file contains hidden or 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 "EmailRepository.h" | |
@interface EmailRepository() | |
@property (nonatomic, copy) void (^onTransactionCompleted)(); | |
@property (nonatomic, copy) void (^onTransactionFailure)(); | |
@end | |
@implementation EmailRepository | |
@synthesize onTransactionFailure; | |
@synthesize onTransactionCompleted; | |
+ (id)sharedEmailRepository { | |
static EmailRepository *emailRepository = nil; | |
static dispatch_once_t onceToken; | |
dispatch_once(&onceToken, ^{ | |
emailRepository = [[self alloc] init]; | |
}); | |
return emailRepository; | |
} | |
- (void)getDataFromServer:(NSString *)emailAddress onComplete:(void (^)(NSString *response))completionBlock onFailure:(void (^)(NSString *response))failureBlock | |
{ | |
// No GCD. | |
//self.onTransactionCompleted = completionBlock; | |
//self.onTransactionFailure = failureBlock; | |
//[self getData]; | |
self.onTransactionCompleted = completionBlock; | |
self.onTransactionFailure = failureBlock; | |
// GCD | |
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); | |
dispatch_async(queue, ^{ | |
// Compute big gnarly thing that would block for really long time. | |
[self getData]; | |
}); | |
} | |
- (void) getData { | |
// Do something with the response. Handle from server for example. | |
BOOL success = YES; | |
NSString *resp = [NSString stringWithFormat:@"%", @"Some server response."]; | |
// Now it is done. Tell the main thread. | |
dispatch_async(dispatch_get_main_queue(), ^{ | |
if(success == true){ | |
if(self.onTransactionCompleted){ | |
self.onTransactionCompleted(resp); | |
} | |
}else{ | |
if(self.onTransactionFailure){ | |
self.onTransactionFailure(resp); | |
} | |
} | |
}); | |
} | |
@end |
This file contains hidden or 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
EmailRepository *sharedEmail = [EmailRepository sharedEmailRepository]; | |
[sharedEmail getDataFromServer:@"test" | |
onComplete:^(NSString *response){ | |
NSLog(@"success"); | |
} onFailure:^(NSString *response){ | |
NSLog(@"failure"); | |
}]; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment