2012-11-02 37 views
0

我試圖從我的服務器獲取更新。我希望它有最新的信息,但這樣我運行一個頁面,如果響應"Complete",而不是"Error"然後我將用更新的方法繼續獲得來自xml文件的最新信息。我的問題是代理方法不會被調用,直到我的「主」功能中的所有代碼完成。那時代碼已經超過了我的if語句,檢查responseSuccess是否爲TRUEFALSE。我認爲它是因爲NSURLConnection是異步的......但我不知道如何解決它。如果我需要提供更多的代碼/信息,請讓我知道。謝謝!iOS:如何在繼續之前執行NSURLConnection並獲得repsonse?

主要

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

回答

1

任何你想要的代碼在從服務器獲取更新需要移動到connectionDidFinishLoading:方法(或從中調用)之後運行。所以,你可以擺脫標誌,只在你的主代碼中有getResponse調用。

相關問題