Skip to content

Instantly share code, notes, and snippets.

@RuiAAPeres

RuiAAPeres/gist:7656683

Last active Dec 29, 2015
Embed
What would you like to do?
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