Last active
September 15, 2015 17:50
-
-
Save casspangell/712d23b4569efe7b561b to your computer and use it in GitHub Desktop.
Completion Block with Data Return
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
.h = - (void)signInAccountWithUserName:(NSString *)userName | |
password:(NSString *)password | |
completion:(void (^)(BOOL success))completionBlock; | |
.m = - (void)signInAccountWithUserName:(NSString *)userName | |
password:(NSString *)password | |
completion:(void (^)(BOOL success))completionBlock | |
{ | |
// Notice that we are passing a BOOL back to the completion block. | |
if (completionBlock != nil) completionBlock(loginSuccessful); | |
} | |
//Call the method: | |
[self signInAccountWithUserName:@"Bob" | |
password:@"BobsPassword" | |
completion:^{ | |
[self doMethod]; //do something like call a method | |
}]; | |
OR | |
[self signInAccountWithUserName:@"Bob" | |
password:@"BobsPassword" | |
completion:^(BOOL success) { | |
if (success) { | |
[self doMethod]; //do something like call a method | |
} else { | |
// do something like display an alert | |
} | |
}]; |
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
//Block that passes in an MKMapView and returns NSData called mapData | |
.h = - (void)ImageFromMapView:(MKMapView*)mapView completion:(void (^)(NSData* mapData))completionBlock; | |
.m = - (void)ImageFromMapView:(MKMapView*)mapView completion:(void (^)(NSData* mapData))completionBlock | |
{ | |
//method does what it needs to do | |
//self.data holds the NSData that was created inside this method | |
completionBlock(self.data); | |
} | |
Calling it = | |
[classWithMethod ImageFromMapView:mapView completion:^(NSData *mapData) { | |
//do something with mapData | |
}]; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment