Does getRowWithData create a new row

Hi,
Does getRowWithData create a new row with all field value passed as parameter ?
Or does it return a row that match all values passed as parameter ?
If the table as 5 fields does it mean that we have to pass 5 value in parameter ? What about autoinc field ?

If this doesn’t create a new row, is there a way to pass all value to a DaDataTableRow in one time ?

Regards

Hi Armindo,

Hm, I can’t recommend using getRowWithData method.
It has quite straightforward implementation
and used by Data Abstract internally for adding bunch of rows to the table in one go.

in contrast to usual addNewRow it
a) doesn’t handle autoincrement values
b) does not add row to the table rows collection
c) doesn’t set modified flag for row fields values.

It’s weird that it exposed as public method. I’ll make it private and adjust description in Wiki.

Re passing all values in the row in one go, no we don’t have that at the moment. I’ll investigate possibility of that
For now I can offer you some kind of workaround:

+(DADataTableRow *)addRowForTable:(DADataTable *)table 
           withDataFromDictionary:(NSDictionary *)data {

  // add new row
  DADataTableRow *row = [table addNewRow];
  
  // loop through all logged fields in the table and write new values
  for (NSUInteger i = 0; i < [[table loggedFields] count]; i++) {
    
    DAFieldDefinition *field = [[table loggedFields] objectAtIndex:i];
    
    // get the proper key & value in data dictionary 
    id key = [field name];
    id value = [data valueForKey:key];
    
    // if value is nil then data doesn't have value for given field. Skipping...
    if (value == nil)
        continue;
    
    
    // if field is autoincrement then addNewRow should already add value there
    BOOL isNullValue = [value isKindOfClass:[DANull class]];
    BOOL isAutoIncrementField = (field.dataType == datAutoInc) 
                             || (field.dataType == datLargeAutoInc);
    if (isAutoIncrementField && isNullValue) 
        continue;
        
    [row setValue:value forKey:key];
  }
  return row;
}

- (IBAction)add:(id)sender {
    
    id autoIncValue = [DANull nullValue];
    
    // if nessesary then uncomment lines below to set *custom* autoincrement value 
    // int lastAutoincValue = -11;
    // autoIncValue = [NSNumber numberWithInt:--lastAutoincValue];
    
    // prepare data dictionary
	NSDictionary *data = [NSDictionary dictionaryWithObjectsAndKeys:
                          autoIncValue, @"WorkerID",
                          @"Bond",      @"WorkerLastName", 
                          @"James",     @"WorkerFirstName", 
                          @"Spy",       @"WorkerPosition",
                          @"TopSecret", @"WorkerNotes", nil];

    // insert new row with data
	DADataTableRow *row = [MyClass addRowForTable:workersTable 
                           withDataFromDictionary:data];
    
    //reload tableview and select added row there
    [tableView reloadData];
    NSIndexSet *selection = 
	   [NSIndexSet indexSetWithIndex:[[workersTable rows] indexOfObject:row]]; 
    [tableView selectRowIndexes:selection byExtendingSelection:NO];
}

Hope it helps.

Thanks for the report!

Thanks Alex, module geting more and more complete :slight_smile: