Reputation: 64864
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
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
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
Upvotes: 1