Reload data of UITableView in background

前端 未结 4 925
夕颜
夕颜 2021-02-04 14:52

In my app, I have a UITableViewController.

Its tableView is divided in 3 sections.

I download datas for each of those sections from my server. To do this, I have

4条回答
  •  时光取名叫无心
    2021-02-04 15:23

    The previous answers are absolutely right. However your implementation of reloadDatas & viewDidLoad is a bit problematic.

    Just to clarify:

    You want to complete the time consuming data loading stuff in a background thread, then update the UI/Cells when your data is ready on the main thread.

    Like so:

      -(void)viewDidLoad
        {
           dispatch_queue_t concurrentQueue = dispatch_queue_create("com.my.backgroundQueue", NULL);
            dispatch_async(concurrentQueue, ^{
                [self reloadDatas];
            });
        }
    
    -(void)reloadDatas
    {
        // Expensive operations i.e pull data from server and add it to NSArray or NSDictionary
          [self f1];
          [self f2];
          [self f3];
    
        // Operation done - now let's update our table cells on the main thread  
    
        dispatch_queue_t mainThreadQueue = dispatch_get_main_queue();
        dispatch_async(mainThreadQueue, ^{
    
            [myDisplayedTable reloadData]; // Update table UI
        });
    }
    

提交回复
热议问题