Why is my loginAuth
stuck inside the while loop?
I declared connectionFinishLoading
as
@interface LoginViewController : UIViewController {
BOOL connectionFinishLoading;
}
@property (nonatomic, assign) BOOL connectionFinishLoading;
@implementation LoginViewController
@synthesize connectionFinishLoading;
-(BOOL)loginAuth {
NSString *requestString = [NSString stringWithFormat:@"http:myURL?id=%@&format=JSON", userName.text];
NSMutableURLRequest *requestURL = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:requestString]];
loginConnection = [[NSURLConnection alloc] initWithRequest:requestURL delegate:self startImmediately:YES];
[SVProgressHUD showInView:self.view status:@"Logging in"];
while (!connectionFin开发者_如何学JAVAishLoading) {
NSLog(@"waiting..");
}
// code to executed after connection did finish loading.
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[responseData appendData:data];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
connectionFinishLoading = YES;
}
Because you block the run loop and no packets are send over the network, thus the connection never finishes.
Try to add this:
- (void)connection:(NSURLConnection *)conn didFailWithError:(NSError *)error
// A delegate method called by the NSURLConnection if the connection fails.
// Production quality code would either display or log the actual error.
{
#pragma unused(conn)
#pragma unused(error)
assert(conn == self.connection);
NSLog(@"didFailWithError %@", error);
connectionFinishLoading = YES;
}
But it is very bad practice to create such loops
As JustSid mentioned, the run loop is being blocked. I would suggest getting rid of the "while" loop and BOOL connectionDidFinishLoading property, and then move your code to be executed into the connectionDidFinishLoading method. You should still handle the connection failed block some way too.
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
// code to executed after connection did finish loading.
}
精彩评论