2014-10-27 56 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 

    }); 
}); 
相关问题