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?