0

サーバーから更新を取得しようとしています。ただし、最新の情報が必要なため、ページを実行しています。応答があった場合とそう"Complete"でない場合"Error"は、Update メソッドを使用してxmlファイルから最新の情報を取得します。私の問題は、「メイン」関数のすべてのコードが完了するまで、デリゲート メソッドが呼び出されないことです。その時までに、コードはすでに if ステートメントresponseSuccessTRUEorであるかどうかをチェックしていFALSEます。NSURLConnection が非同期だからだと思いますが、修正方法がわかりません。さらにコードや情報を提供する必要がある場合は、お知らせください。ありがとう!

主要

    if (UpdatingFlag)
    {
        NSLog(@"Cannot update while updating is in process...");
    } else {
        // run updates before getting information
        [self getResponse:@"http://url/path/to/file?function=myvalue"];

        [self setBottomBarToUpdating:@"Processing Please Wait..."];

        dispatch_queue_t queue = dispatch_queue_create("updateQueue", DISPATCH_QUEUE_CONCURRENT);

        UpdatingFlag = TRUE;

        if(responseSuccess)
        {
           dispatch_async(dispatch_get_main_queue(),^ {
              [self setBottomBarToUpdating:@"Updating..."];
              [self updateFromXMLFile:@"https://url/path/to/file.xml"];
           });
        }

        UpdatingFlag = FALSE;

        dispatch_barrier_async(queue,^ {
            dispatch_async(dispatch_get_main_queue(), ^{
                 [self setBottomBarToUpdated];
            });
        });

}

GetReponse メソッド

- (void) getResponse:(NSString *)url
{
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]
                                             cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:45.0];
    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
    [connection start];

    if (connection) {NSLog(@"Connecting...");
    } else {NSLog(@"Didn't Connect Error...");}
}

デリゲート メソッド

#pragma mark NSURLConnection methods

- (void)connection:(NSURLConnection *)conn didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
    NSLog(@"Did Receive Challenge");

    if ([challenge previousFailureCount] == 0) {
        NSURLCredential *newCredential;
        newCredential = [NSURLCredential credentialWithUser:@"user" password:@"pass" persistence:NSURLCredentialPersistenceNone];
        [[challenge sender] useCredential:newCredential forAuthenticationChallenge:challenge];
    } else {
        [[challenge sender] cancelAuthenticationChallenge:challenge];

        NSLog(@"Invalid Username and Password");

        UIAlertView * userNameAlert = [[UIAlertView alloc]initWithTitle:@"Error"
                                                                message:@"ErrorMsg"
                                                               delegate:self
                                                      cancelButtonTitle:nil
                                                      otherButtonTitles:@"OK", nil];
        [userNameAlert show];

    }

}

- (void)connection:(NSURLConnection *)conn didReceiveData:(NSData *)data {
    NSLog(@"Received Data Packet...");
    response = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    NSLog(@"Error, %@", error);
}

- (void)connectionDidFinishLoading:(NSURLConnection *)conn {
    NSLog(@"Finished Loading");
    if([response rangeOfString:@"Complete"].location == NSNotFound) {
        // success
        responseSuccess = FALSE;
    } else {
        // failed
        responseSuccess = TRUE;
    }
}

- (BOOL)connection:(NSURLConnection *)conn canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace {
    return YES;
}
4

1 に答える 1

0

サーバーから更新を取得した後に実行するコードはすべて、connectionDidFinishLoading: メソッドに移動 (または呼び出し) する必要があります。したがって、フラグを削除して、メイン コードで getResponse 呼び出しのみを行うことができます。

于 2012-11-02T15:53:07.127 に答える