aneuryzm
aneuryzm

Reputation: 64864

NSUrlConnection doesn't work. I don't get any response

In my class I create a CBUrlConnection:

urlConnection = [NSURLConnection connectionWithRequest:request delegate:self];
[urlConnection start];

but I don't get any response, these delegate methods are never invoked:

- (void)connection:(NSURLConnection *)connection didFinishLoadingData:(NSData *)data error:(NSError *)error
- (void)connection:(NSURLConnection *)connection didUpdateProgress:(CGFloat)percent
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response

The request seems to be correct, the server url is surely correct, and the server is working correctly. What could be wrong ?

Upvotes: 1

Views: 763

Answers (2)

aneuryzm
aneuryzm

Reputation: 64864

I've just fixed it... it was because the code wasn't running on the main thread.

For who might be interested I've solved with:

dispatch_sync(dispatch_get_main_queue(), ^{

});

Upvotes: 4

Johannes Fahrenkrug
Johannes Fahrenkrug

Reputation: 44836

You can absolutely run an NSURLConnection from inside an NSOperation. The actual trick is that an NSURLConnection needs a run loop. See http://www.cocoaintheshell.com/2011/04/nsurlconnection-synchronous-asynchronous/ and https://stackoverflow.com/a/6238764/171933

  • Johannes

Upvotes: 1

Related Questions