Created
July 31, 2014 06:34
-
-
Save kazukitanaka0611/8823a3c5c8421e20aba5 to your computer and use it in GitHub Desktop.
My method NSURLSession blocks
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
- (void)getRequestData:(NSString *)urlString | |
success:(void(^)(NSArray *jsonList))success | |
failure:(void(^)(NSError *error))failure | |
{ | |
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration]; | |
NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration]; | |
NSURLSessionDataTask *task = [session dataTaskWithURL: | |
[NSURL URLWithString:urlString] | |
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { | |
NSLog(@"error %@", error); | |
if (error != nil) | |
{ | |
failure(error); | |
return; | |
} | |
NSArray *myList = [NSJSONSerialization JSONObjectWithData:data | |
options:NSJSONReadingAllowFragments | |
error:nil]; | |
if([NSJSONSerialization isValidJSONObject:myList]) | |
{ | |
DDLogInfo(@"myList ========================= %@", myList); | |
success(myList); | |
NSMutableArray *array = [[NSMutableArray alloc] init]; | |
for (NSDictionary *json in myList) | |
{ | |
TiqavDataModel *model = [[TiqavDataModel alloc] initWithDictionary:json error:nil]; | |
[array addObject:model]; | |
} | |
self.list = array; | |
[self.collectionView reloadData]; | |
} | |
else | |
{ | |
NSError *error = [NSError errorWithDomain:@"MyDomain" | |
code:9999 | |
userInfo:@{@"message" : @"json parase error"}]; | |
failure(error); | |
} | |
}]; | |
[task resume]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment