卡在这个 while 循环中

发布于 2024-12-02 15:02:20 字数 1140 浏览 0 评论 0原文

为什么我的 loginAuth 卡在 while 循环中?

我将 connectionFinishLoading 声明为

@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 (!connectionFinishLoading) {
        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;
}

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 (!connectionFinishLoading) {
        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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

你是我的挚爱i 2024-12-09 15:02:20

因为您阻塞了运行循环并且没有数据包通过网络发送,因此连接永远不会完成。

Because you block the run loop and no packets are send over the network, thus the connection never finishes.

无远思近则忧 2024-12-09 15:02:20

尝试添加这个:

- (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;
}

但是创建这样的循环是非常糟糕的做法

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

笨笨の傻瓜 2024-12-09 15:02:20

正如 JustSid 提到的,运行循环被阻塞。我建议摆脱“while”循环和 BOOL connectionDidFinishLoading 属性,然后将要执行的代码移动到 connectionDidFinishLoading 方法中。您仍然应该以某种方式处理连接失败块。

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    // code to executed after connection did finish loading.
}

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.
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文