Deleting Tasks

Deleting Tasks

The CRUD operation you need to implement is Delete.

DataAccess

Start by adding a new removeTask method to the DataAccess object.

//DataAccess.m
- (void)removeTask:(DADataTableRow *)task 
{
    [self.tasksTable removeRow:task];
}

The removeTask method is, in this case, very simple and just removes the DADataTableRow from the DADataTable using the removeRow method.

AppDelegate

Once again add a new button to the toolbar and link it up to the IBAction method deleteTaskAction.

//AddDelegate.m
- (IBAction)deleteTaskAction:(id)sender {
  id t = [[self.tableController selectedObjects] lastObject];
  if (!t)
    return;
 
  DADataTableRow *task = (DADataTableRow *)t;
 
  NSAlert *alert = [NSAlert alertWithMessageText:@"Confirm Remove Action"
                                   defaultButton:@"Remove"
                                 alternateButton:@"Cancel"
                                     otherButton:nil
                       informativeTextWithFormat:@"Do you really want to remove the task #%@", task[@"Id"]];
 
  [alert beginSheetModalForWindow:[self.tableView window]
                completionHandler:^(NSModalResponse returnCode) {
                  if (returnCode == NSAlertDefaultReturn) {                          
                    id task = [[self.tableController selectedObjects] lastObject];
                    [[DataAccess sharedInstance] removeTask:task];
                    [[DataAccess sharedInstance] beginApplyUpdates];
                    [self.tableController rearrangeObjects];
                  }    
                }];
}

The deleteTaskAction just gets the selected DADataTableRow from the DADataTableController and shows an alert sheet. If the user selects the default button ("Remove") on the sheet, the removeTask method of the DataAccess is called to remove the row from the DADataTable, followed by the beginApplyUpdates method to save the changes back to Relativity Server.

Delete a Task