Skip to content

Instantly share code, notes, and snippets.

@RuiAAPeres
Last active December 29, 2015 10:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RuiAAPeres/7656683 to your computer and use it in GitHub Desktop.
Save RuiAAPeres/7656683 to your computer and use it in GitHub Desktop.
The parseData is the private method, where the parsing actually works (NSData => NSDictionary). The other one parseLoginData (the public method), builds the model object from the Dictionary.
+ (void)parseLoginData:(NSData *)responseData withCompletionBlock:(MJDataSourceCompletionBlock)completionBlock
{
[self parseData:responseData withCompletionBlock:^(id response, MJError *error)
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void)
{
MJLoginResponse *loginResponse = [[MJLoginResponse alloc] initWithLoginResponse:response];
dispatch_async(dispatch_get_main_queue(), ^(void)
{
if(loginResponse && !error)
{
completionBlock(loginResponse,nil);
}
else
{
MJError *mjError = [[MJError alloc] initWithCode:MJDefaultError withMessage:nil];
completionBlock(nil,mjError);
}
});
});
}];
}
#pragma mark - General Case Parsing
+ (void)parseData:(NSData *)responseData withCompletionBlock:(MJDataSourceCompletionBlock)completionBlock
{
dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void)
{
NSError *error = nil;
id parsingResult = [NSJSONSerialization JSONObjectWithData:responseData options: NSJSONReadingMutableContainers error: &error];
dispatch_async(dispatch_get_main_queue(), ^(void)
{
if(error)
{
MJError *mjError = [[MJError alloc] initWithCode:MJDefaultError withMessage:nil];
completionBlock(nil,mjError);
}
else
{
completionBlock(parsingResult,nil);
}
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment