-(void )getDataFromServer: (NSMutableDictionary *)dict
{
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@/doSomething",MainURL ]];
[AFJSONRequestOperation addAcceptableContentTypes:[NSSet setWithObject:@"text/html"]];
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];
NSMutableURLRequest *request = [httpClient requestWithMethod:@"POST" path:nil parameters:dict];
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request
success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON)
{
_myArray = JSON;
[_myTableView reloadData]; //Or do some other stuff that are related to the current `ViewController`
}
failure:^(NSURLRequest *request , NSURLResponse *response , NSError *error , id JSON)
{
NSLog(@"request: %@",request);
NSLog(@"Failed: %@",[error localizedDescription]);
}];
[httpClient enqueueHTTPRequestOperation:operation];
}
I'm using the above code in 7 different places in one of my apps. The exact chunk of code is duplicated in 7 of my ViewControllers
. What I normally used to do is to put the method I want to use in a NSObject class and allocate and use it when ever I need but because the above is Async and using blocks I can't just return the JSON to the ViewController
who called it and have to copy & paste the above method in every ViewController
I need it in.
My goal is to have the above in only one place in my app and still be able to call it from different ViewControllers
around my app and get the data I need.
I'd like to avoid using an observer such as NSNotification
or KVO
and looking for a more elegant solution.
After some reading I noticed the possibility to pass blocks around. Is that a possible solution with the above? A code example would be appreciated.