Pra Do
Pra Do

Reputation: 293

Not able to fetch JSON data in iOS

I will post my code here:

NSString *string=[NSString stringWithFormat:@"https://api.fda.gov/drug/event.json?limit=25"];

NSLog(@"string:%@",string);

NSURL *url=[NSURL URLWithString:string];

NSLog(@"url %@",url);

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
 NSData *data=[NSData dataWithContentsOfURL:url];

    NSLog(@"data:%@",data);

    [self performSelectorOnMainThread:@selector(fetcheddata:) withObject:data waitUntilDone:YES];
});

-(void)fetcheddata:(NSData *){}

The above code worked for me earlier. I don't know what happened it's not working now. When I pasted the URL in the browser it is working. Please guide me in this case. It shows me data parameter is nil and exception is NSInvalidArgumentException.

Upvotes: 0

Views: 111

Answers (1)

Hussain Shabbir
Hussain Shabbir

Reputation: 15035

Change the below line

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

with

dispatch_sync(dispatch_get_main_queue(), ^{

Upvotes: 0

Related Questions