2014-05-23 38 views
0

我在製作一個應用程序,它使用NSURLSession從「php後端」獲取數據。我使用NSLog來檢查獲取數據需要多少時間,並且數據幾乎立即生效。但是,一旦我將它連接起來以更改UILabel的文本,文本就需要幾秒鐘才能更改。 NSURLSession是否應該將提取權限降低到後臺線程,以便UI交互更快? 另外,有什麼辦法可以加快速度?這裏是我的viewController代碼: -NSURLSession快速加載,但UI更改時間太長

// 
// hoursViewController.m 
// NSS IITD 
// 
// Created by Robin Malhotra on 22/05/14. 
// Copyright (c) 2014 Robin's code kitchen. All rights reserved. 
// 

#import "hoursViewController.h" 

@interface hoursViewController() 
@property (strong, nonatomic) IBOutlet UILabel *hoursLabel; 
@property (strong,nonatomic) NSArray *dataArray; 
@end 


@implementation hoursViewController 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) { 
     // Custom initialization 
    } 
    return self; 
} 
- (IBAction)checkHours:(id)sender 
{ 
    NSString *[email protected]"http://www.nssiitd.in/nsshours/teststudent.php?entry_no=2012TT10951"; 
    NSURLSession *session = [NSURLSession sharedSession]; 
    [[session dataTaskWithURL:[NSURL URLWithString:URL] 
      completionHandler:^(NSData *data, 
           NSURLResponse *response, 
           NSError *error){ 
       NSString *dataString=[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; 
       NSLog(@"%@",dataString); 
       self.dataArray=[dataString componentsSeparatedByString:@";"]; 

       for (NSString *string in self.dataArray) { 

        NSLog(@"%@",string); 

       } 

       NSLog(@"lasdt valiuse is %@",[self.dataArray objectAtIndex:2]); 
       UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Hours" message:[self.dataArray objectAtIndex:4] delegate:self cancelButtonTitle:@"okay" otherButtonTitles: nil]; 
       [alert show]; 
       [self.hoursLabel setText:[self.dataArray objectAtIndex:4]]; 

      }] resume]; 


} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 




    // Do any additional setup after loading the view. 
} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

/* 
#pragma mark - Navigation 

// In a storyboard-based application, you will often want to do a little preparation before navigation 
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{ 
    // Get the new view controller using [segue destinationViewController]. 
    // Pass the selected object to the new view controller. 
} 
*/ 

@end 

和 「Here's」 上 「GitHub上」 整個項目。

回答

3

嘗試在主線程調度UI代碼:

[[session dataTaskWithURL:[NSURL URLWithString:URL] 
     completionHandler:^(NSData *data, 
          NSURLResponse *response, 
          NSError *error){ 
      NSString *dataString=[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; 
      NSLog(@"%@",dataString); 
      self.dataArray=[dataString componentsSeparatedByString:@";"]; 

      for (NSString *string in self.dataArray) { 

       NSLog(@"%@",string); 

      } 

      NSLog(@"lasdt valiuse is %@",[self.dataArray objectAtIndex:2]); 

      dispatch_async(dispatch_get_main_queue(), ^{ 
       UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Hours" message:[self.dataArray objectAtIndex:4] delegate:self cancelButtonTitle:@"okay" otherButtonTitles: nil]; 
       [alert show]; 
       [self.hoursLabel setText:[self.dataArray objectAtIndex:4]]; 
      }); 

     }] resume]; 
+0

太謝謝你了。不幸的是,我不能投票給你,因爲我沒有信譽 –

+0

沒問題!我很高興能夠提供幫助。 – Pablo

+0

@RobinThuranMalhotra如果這解決了你的問題(它應該)一定要接受答案,這樣其他人才不會對它刮目相看。 –