Last active
December 29, 2015 10:19
-
-
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.
This file contains 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)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