I have some code that makes a http call to a json based webservice. That's working fine but I tried to move the code to it's own class and I have a slight hang up. When I call the method, the main thread just continues to the next command without wait for a response from my method.
Here's some code from the main part of the program
[newcall run];
NSLog(@"%@",[newcall status]);
NSArray *resultarray= [newcall returndata];
for (NSString *element in resultarray) {
NSLog(@"%@",element);
}
My Header
#import "AFHTTPClient.h"
@interface jsoncall : AFHTTPClient
{
NSString* Date;
NSString* apps;
NSString* data1;
NSURL* url;
NSString* Path;
NSArray* returndata;
NSString* status;
}
-(void) setApp: (NSString *)input;
-(void) setData: (NSString *)input;
-(void) setURL: (NSString *)input;
-(void) setPath: (NSString *)input;
-(int) run;
-(NSArray *) returndata;
-(NSString *) status;
@end
My run method
-(int) run
{
__block int success;
NSDictionary* jsonDictionary=[NSDictionary dictionaryWithObject: data1 forKey:@"data"];
NSString* jsonString = [jsonDictionary JSONRepresentation];
AFHTTPClient *httpClient=[[AFHTTPClient alloc] initWithBaseURL:url];
NSDictionary *params =[NSDictionary dictionaryWithObjectsAndKeys:
apps,@"app",
jsonString,@"smpdata",nil];
NSMutableURLRequest *request = [httpClient requestWithMethod:@"POST" path:Path parameters:params];
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request
success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
NSArray *dataarray=[JSON valueForKey:@"Data"];
status= [NSString stringWithFormat:@"%@",[JSON valueForKeyPath:@"Status"]];
NSLog(@"%@",status);
returndata= dataarray;
success=1;
NSLog(@"Success: Made it here");
}
failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error,id JSON)
{
success=0;
NSLog(@"Error: Made it here");
}
];
[operation start];
[operation waitUntilFinished];
return success;
}