Skip to content

Instantly share code, notes, and snippets.

@kazukitanaka0611
Created July 31, 2014 06:34
Show Gist options
  • Save kazukitanaka0611/8823a3c5c8421e20aba5 to your computer and use it in GitHub Desktop.
Save kazukitanaka0611/8823a3c5c8421e20aba5 to your computer and use it in GitHub Desktop.
My method NSURLSession blocks
- (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