Can anyone tell me why I can’t return a NSMutableArray there:
-(NSMutableArray)makeServiceRequest:(UIViewController *)sender
{
NSMutableString *urlString= [NSString stringWithFormat:@"http://www.servicedata/%@", _searchValue];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSData* data = [NSData dataWithContentsOfURL:
[NSURL URLWithString: urlString]];
NSError* error;
NSMutableArray *json = [NSJSONSerialization JSONObjectWithData:data
options:kNilOptions
error:&error];
dispatch_async(dispatch_get_main_queue(), ^(){
NSLog(@"json arrived");
return json;
});
});
}
I’ve it well declared on the header file. And is there any way to turn that into a class method?
You cant do this, you dispatch block is equivalent to creating another thread and letting it go, by the time your block executes the method makeServiceRequest has already executed and is no longer in scope, what you need to do it notify the caller that their request is done via protocols or notifications… the flow goes like
If you want to learn about NSNotifications check out this questions