NSURLSession加载速度很快,但UI更改需要很长时间

时间:2014-05-23 15:00:01

标签: php ios objective-c

我正在创建一个使用NSURLSession从“php后端”获取数据的应用。我使用NSLog来检查获取数据所需的时间,并且数据几乎立即进入。但是,一旦我将其连接起来以更改UILabel的文本,文本更改需要几秒钟的时间。是不是NSURLSession应该将获取权转移到后台线程,因此UI交互更快? 另外,有什么方法可以加快速度吗?这是我的viewController代码: -

    //
//  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 *URL=@"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”的整个项目。

1 个答案:

答案 0 :(得分: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];