0
votes

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.

1
Your code works for me, I always get data after like 3 seconds. - gabbler
I created new project and wrote the code again in viewdidload but still it doesn't work for me. I am using Xcode 6 iOS8. - Pra Do
I am also use Xcode 6 and iOS 8, can you share your code? - gabbler

1 Answers

0
votes

Change the below line

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

with

dispatch_sync(dispatch_get_main_queue(), ^{