Refreshing the Data

Refreshing the Data

So far your sample application will only load data from the Relativity Server on startup. Obviously, in a real world application, you are going to need to refresh that data from time to time, so that's what you are going to implement next.

Add a toolbar

Add a Toolbar to your main window and add a single button with the title Reload. Add a new IBAction method called reloadTasksAction: to AppDelegate.m and link it up so it will be called when your new Reload button is pressed.

Add a Relload Button

Refreshing

It doesn't really make sense to refresh all the data every time the Reload button is pressed. The data in the priorities table is very unlikely to have changed, so only loading it at startup is probably acceptable. This means you actually only need to refresh the data from the tasks table.

Add a new method to DataAccess.m called reloadTasks and implement the code below.

//DataAccess.m
- (void)reloadTasks {
  // Use GCD to run this action in the background
  dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
    // synchronous call of the RemoteDataAdapter
    DADataTable *t = [rda getDataTable:@"Tasks"];

    // Process results in the main thread.
    dispatch_async(dispatch_get_main_queue(), ^{
      // in order to preserve calculated and lookup fields in our table
      // we can just replace rows there with new rows we just got
      [self.tasksTable replaceRowsWithDataFromTable:t];
      [self triggerDataReady];
    });
  });
}

The method uses GCD to ensure that the data refresh does not happen on the main thread. The data is refreshed by asking the DARemoteDataAdapter to get the data for the tasks table.

You could then just replace the DADataTable in the tasksTable property of the DataAccess object with the new DADataTable that has just been created by the download, but that would mean that you would also need to create both the lookup and calculated fields again. To avoid having to do this, it is better to just ask the DADataTable in the tasksTable property to replace its data with the data in the new DADataTable using the replaceRowsWithDataFromTable method.

You need to do this on the main thread to ensure it doesn't clash with anything the UI might be doing with the table. Once the update is complete, call the triggerDataReady method on the DataAccess object, which will fire a notification that, in turn, will trigger the UI to refresh.