2014-10-27 44 views
0

我有一個建立在Xcode中的應用,有一個按鈕註銷用戶,它包含下面的代碼,我有沒有問題:我的代碼不排隊因爲我想要

self.LbGoodBye.Text = @"Goodbye"; 

NSString *post =[[NSString alloc] initWithFormat:@"myQasidaName=%@",[self.textUserName text]]; 
NSLog(@"PostData: %@",post); 
NSURL *url=[NSURL URLWithString:@"http:/MyWebSite/logOutUserDefaults.php"]; 
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]; 
NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[postData length]]; 
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; 

[request setURL:url]; 
[request setHTTPMethod:@"POST"]; 
[request setValue:postLength forHTTPHeaderField:@"Content-Length"]; 
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"]; 
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; 
[request setHTTPBody:postData]; 
NSError *error = [[NSError alloc] init]; 
NSHTTPURLResponse *response = nil; 
NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; 
NSLog(@"Response code: %ld", (long)[response statusCode]); 

if ([response statusCode] >= 200 && [response statusCode] < 300) { 

NSString *responseData = [[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding]; 
NSLog(@"Response ==> %@", responseData); 
NSString *url = [NSString stringWithFormat:@"http:/MyWebSite/logOutUserDefaults.php?qasidaName=%@",[self.textUserName text]]; 
NSData *data =[[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:[url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]]; 
self.jasonArray = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil]; 

[self performSegueWithIdentifier:@"GoToMain" sender:self]; 

我有一個標籤,我想用它作爲向用戶說再見的信息,問題是當我把代碼放在乞討處,直到剩下的代碼完成時它才工作。

我不知道如果我有Xcode的一個問題,我試圖建立新的應用程序,但仍然有,我使用的Xcode 6.0.1同樣的問題,我開始建立使用Xcode的5

這個程序

我是Xcode的新手,我沒有那麼多經驗來解決這個問題。

任何人都可以幫助我嗎?

回答

0

你在應用程序主線程中做了你的代碼,這就是爲什麼直到它的過程沒有完成它將不會在屏幕上顯示標籤更改。

使用後臺線程進行呼叫註銷Web服務。下面是後臺調用API的示例。

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void){ 
    //Background Thread add your logout api call here 
    dispatch_async(dispatch_get_main_queue(), ^(void){ 
     //Run UI Updates [self performSegueWithIdentifier:@"GoToMain" sender:self]; add this code over here 

    }); 
}); 
相關問題