0

So, I am trying to connect to a remote server to get and show data. in viewDidLoad I use an NSThread to call a function called doSomething

- (void)doSomething
{        
    @autoreleasepool
    {                
        NSMutableURLRequest *httpRequest = [NSMutableURLRequest requestWithURL:someURL];
        [httpRequest setHTTPMethod:@"POST"];
        [httpRequest setValue:[NSString stringWithFormat:@"%d", httpRequestParametersClean.length] forHTTPHeaderField:@"Content-Length"];
        [httpRequest setValue:@"application/x-www-form-urlencoded charset=utf-8" forHTTPHeaderField:@"Content-Type"];
        [httpRequest setHTTPBody:httpRequestParametersClean];

        (void)[[NSURLConnection alloc] initWithRequest:httpRequest delegate:self];

        for (NSString* key in response)
        {
             // loop through returned values
        }
     }
}

The code in viewDidLoad is

 [NSThread detachNewThreadSelector:@selector(someURL) toTarget:self withObject:nil];

Then I have a REFRESH button which when clicked calls doSomething as well by simply saying [self doSomething]

My problem is that when view is loaded, the response from server comes empty. I still get no response until I click on the refresh button. Strange! What am I doing wrong?

Eyad Fallatah
  • 1,859
  • 4
  • 22
  • 34

1 Answers1

3

A NSURLConnection created with initWithRequest:delegate: works asynchronously, calling the delegate functions connection:didReceiveResponse:, connection:didReceiveData:, ... later, when data is read from the server. Your code does not even start the connection, so nothing will happen anyway.

The easiest way to fix your problem is to use the synchronous version

sendSynchronousRequest:returningResponse:error:

of NSURLConnection. If doSomething is executed in a separate thread, this will not block the UI.

Added: (Thanks to @geowar for mentioning this.) Note that you can also use the delegate-based NSURLConnection methods. These are more flexible (see e.g. https://stackoverflow.com/a/15591636/1187415 for a comparison). Another good choice is sendAsynchronousRequest:queue:completionHandler:, which creates a background thread automatically.

Community
  • 1
  • 1
Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
  • doesn't (void)[[NSURLConnection alloc] initWithRequest:httpRequest delegate:self]; start the connection? – Eyad Fallatah Mar 31 '13 at 18:35
  • `connectionWithRequest:delegate:` and `initWithRequest:delegate:startImmediately:YES` start the connection immediately, but not `initWithRequest:delegate:`. – Martin R Mar 31 '13 at 19:02
  • While this is the correct answer to the question asked, a better solution would be to use the asynchronous method and the delegate callback methods. – geowar Apr 01 '13 at 17:31
  • @geowar: The asynchronous method is more flexible, but if you don't need these advantages then the synchronous method on a background thread works as well. There is also `sendAsynchronousRequest:queue:completionHandler:`, which saves you from creating a thread. – Martin R Apr 01 '13 at 17:47
  • @geowar: Thank you for your feedback, I have added your and my comments to the answer. – Martin R Apr 01 '13 at 18:22