So I have some code like so:
@interface RequestHandler()
@property (nonatomic) NSInteger statusCode;
@end
@implementation RequestHandler
- (bool)sendRequest:(NSString *)surveyorId withData:(NSData *)requestData
{
[[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:true];
if (self.statusCode == 200)
{
return YES;
}
return NO;
}
Clearly the routine will carry on into the if-else
statement before the request has finished. Therefore, self.statusCode
is not set properly in the delegate didReceiveResponse
before it is checked. What would be the best way of doing this?
I am just thinking of adding another bool
property that will be set in connectionDidFinishLoading
and then loop until this property is set. Once it has done that, then it will check self.statusCode
. However I am thinking this will block the thread will it not? It will be no different from a sendSynchronousRequest
right? Is there any way to do this without putting it into a background thread?