0

わかりました、これは非常に単純なはずです - 私がしたいのは、私の ServerConnect.m (NSObject)、私の SignIn.m (ViewController) から 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

1つの提案は次のようにしてください:

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

これを試すことができます。終了するとブロックが使用されます。私はここに似たようなものを持っていました。

// 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 に答える