简体   繁体   English

加载ViewController之前先加载ViewController

[英]Loading ViewController before loading Data is IOS

In my ViewController I load my TableView data from a mySQL server online. 在ViewController中,我从MySQL服务器在线加载TableView数据。 When I press the button that load the ViewController, it takes time before loading the ViewController. 当我按下加载ViewController的按钮时,加载ViewController需要一些时间。

Although my data is being called after viewDidLoad method .. 虽然我的数据在viewDidLoad方法之后被调用..

- (void)viewDidLoad {
    [super viewDidLoad];

    MyArray = [[NSMutableArray alloc]initWithCapacity:10];
    MyData = [NSUserDefaults standardUserDefaults];

    MySqlDataBase *mySql = [[MySqlDataBase alloc]init];
    MyArray = [mySql doQueryGetArray:@"SELECT * FROM member" ForDataBaseWithName:@"Estifta"];
    NSLog(@"%@",[[MyArray objectAtIndex:0]objectForKey:@"text"]);
    [_myTabeView reloadData];
}

viewDidLoad is called when your view is loaded into the memory but before the view is displayed on screen. 在将视图加载到内存中但在屏幕上显示视图之前,将调用viewDidLoad Your code in viewDidLoad runs on the mainThread (UIThread) and therefore slows the display of the view on screen. viewDidLoad中的代码在mainThread(UIThread)上运行,因此会减慢屏幕上视图的显示速度。

As solution you could dispatch the long running parts on a background queue (using dispatch_async) and afterwards reload the tableview on the main thread. 作为解决方案,您可以将长时间运行的部分分派到后台队列中(使用dispatch_async),然后在主线程上重新加载tableview。

    __weak typeof(self) weakSelf = self;
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
MySqlDataBase *mySql = [[MySqlDataBase alloc]init];        
MyArray = [mySql doQueryGetArray:@"SELECT * FROM member" ForDataBaseWithName:@"Estifta"];
        dispatch_async(dispatch_get_main_queue(), ^{
            [weakSelf.myTabeView reloadData];
        });
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM