0

好的,这看起来应该很简单——我要做的就是从我的 SignIn.m(ViewController)调用我的 ServerConnect.m(NSObject)、NSURL 连接请求方法,并在 NSURL 请求完成后停止 UIActivityIndi​​catorView。当然,如果我在主线程上完成所有操作:

- (IBAction)forgotPassword:(id)sender {    
    [activityIndicator startAnimating];
    connection = [[ServerConnect alloc] init];
    [connection sendUserPassword:email withSecurity:securityID];
    [activityIndicator stopAnimating];
}

然后,一切都将同时执行,并且活动指示器将在连接方法完成之前启动和停止......

因此,我试图将连接请求放在辅助线程上:

- (IBAction)forgotPassword:(id)sender {
    [NSThread detachNewThreadSelector: @selector(requestNewPassword:) toTarget:self withObject:userEmail.text];
}

- (void) requestNewPassword:(NSString *)email
{
    [self->thinkingIndicator performSelectorOnMainThread:@selector(startAnimating) withObject:nil waitUntilDone:NO];

    //Make NSURL Connection to server on secondary thread
    NSString *securityID = [[NSString alloc] init];
    securityID = @"security";
    connection = [[ServerConnect alloc] init];
    [connection sendUserPassword:email withSecurity:securityID];

    [self->thinkingIndicator performSelectorOnMainThread:@selector(stopAnimating) withObject:nil waitUntilDone:NO];
}

但是,我在这里也看不到活动指示器,这可能是由于 NSURL 请求在辅助线程上无法正常运行(即,由于某种原因,它没有像在主线程上请求时那样收集 xml 字符串) .

构建我的代码以使其工作的正确方法是什么?我很惊讶在尝试弄清楚如何让我的活动指示器在另一个文件中的方法完成执行后简单地停止时涉及了多少工作。有没有办法串联(一个接一个)而不是同时运行代码?任何帮助,将不胜感激。

更新为显示:sendUserPassword:(NSString *)withSecurity:(NSString *)

- (void)sendUserPassword:(NSString *)emailString
            withSecurity:(NSString *)passCode;
{
    NSLog(@"Making request for user's password");
    newUser = NO;
    fbUser = NO;
    forgotPassword = YES;
    NSString *post = [NSString stringWithFormat: @"email=%@&s=%@", emailString, passCode];
    NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding];
    //Construct the web service URL
    NSURL *url = [NSURL URLWithString:@"http://www.someurl.php"];
    //Create a request object with that URL
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
                                                           cachePolicy:NSURLRequestReloadIgnoringCacheData
                                                       timeoutInterval:90];
    [request setURL:url];
    [request setHTTPMethod:@"POST"];
    [request setHTTPBody:postData];
    //Clear out the existing connection if there is one
    if(connectionInProgress) {
        [connectionInProgress cancel];
    }
    //Instantiate the object to hold all incoming data
    xmlData = [[NSMutableData alloc] init];

    //Create and initiate the conection - non-blocking
    connectionInProgress = [[NSURLConnection alloc] initWithRequest: request
                                                           delegate:self
                                                   startImmediately:YES];
}
4

3 回答 3

0

一个建议是这样尝试:

- (IBAction)forgotPassword:(id)sender
{
    [self->thinkingIndicator startAnimating];
    [NSThread detachNewThreadSelector: @selector(requestNewPassword:) toTarget:self withObject:userEmail.text];
}

- (void) requestNewPassword:(NSString *)email
{
    //Make NSURL Connection to server on secondary thread
    NSString *securityID = [[NSString alloc] init];
    securityID = @"security";
    connection = [[ServerConnect alloc] init];
    [connection sendUserPassword:email withSecurity:securityID];

    [self->thinkingIndicator performSelectorOnMainThread:@selector(stopAnimating) withObject:nil waitUntilDone:NO];
}
于 2013-06-14T08:13:46.683 回答
0

我最终合并了 NSNotification 系统(请参阅 iOS 的多线程)来解决我的问题。不赞成这样做的任何原因:

“将更新从代码的一部分发送到另一部分的一种简单方法是 Apple 内置的 NSNotification 系统。

这很简单。你得到 NSNotificationCenter 单例(通过 [NSNotificationCenter defaultCenter])并且:

1.) 如果您有要发送的更新,请调用 postNotificationName。你只需给它一个你组成的唯一字符串(例如“com.razeware.imagegrabber.imageupdated”)和一个对象(例如刚刚完成下载其图像的 ImageInfo)。

2.) 如果你想知道这个更新什么时候发生,你调用 addObserver:selector:name:object。在我们的例子中,ImageListViewController 会想知道什么时候发生这种情况,以便它可以重新加载适当的表格视图单元格。在 viewDidLoad 中放置它的好地方。

3.) 当视图被卸载时不要忘记调用 removeObserver:name:object。否则,通知系统可能会尝试在未加载的视图(或更糟的是未分配的对象)上调用方法,这将是一件坏事!”

于 2013-06-16T01:20:56.793 回答
0

你可以试试这个,它完成后使用一个块。我在这里也有类似的事情。

// Turn indicator on

// Setup the request
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlString]];
[request setTimeoutInterval: 90.0];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:postData];
request.cachePolicy = NSURLRequestReturnCacheDataElseLoad;

[NSURLConnection sendAsynchronousRequest:request
              queue:[NSOperationQueue currentQueue]
              completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
              // Its has finished but sort out the result (test for data and HTTP 200 i.e. not 404)
              NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
              if (data != nil && error == nil && [httpResponse statusCode] == 200)
              {

                 // Connection finished a gooden
                 // Do whatever you like with data
                 // Stop indicator

              }
              else
              {
                 // There was an error, alert the user
                 // Do whatever you like with data
                 // Stop indicator

              }

          }];
于 2013-07-18T13:44:26.170 回答