In the last release we have introduced two new methods in the DADataTable which allows adding new row with data in one go
The first one is public -addNewRowWithDataFromDictionary:inEditMode:
It returns new row with values from specified dictionary which contains key-value pairs with field name as the key and value – as value for given field.
NSDictionary*data = @{@"WorkerBirthDate": date, @"WorkerLastName":@"Doe", @"WorkerPosition":@"Boss", @"WorkerFirstName":@"John"}; DADataTableRow *row =[table addNewRowWithDataFromDictionary:data inEditMode:NO]; |
This method also perform some basic validation. If you will specify value for field which does not belongs to the table then it throws an exception saying that given field does not exists in the table.
I also need to say few words about adding rows for tables with autoincrement fields. As you might know, when you insert new row to the table with autoincrement key, Data Abstract, at the client side, will provide temporary key value. It is an negative integer value in order not to interfere with existing positive values.
-addNewRowWithDataFromDictionary:inEditMode: method does not allow you to specify custom value for such autoincrement key. Though it does not firbid to change temporary key value before sending an updateData request.
To avoid ambiguity, if you include value for autoinc field to the dictionary then adding new row will throw an exception saying that you cannot specify custom value for autoincrement field.
So general recomendation here will be – If your table has an autoincrement key field then just omit passing its value. It will be generated automatically.
And finally you don’t need to pass values for ALL fields. You can specify only ones you need. It also can be handy for setting defaults.
The second method -addNewRowWithDataFromArray:inEditMode: is less safe. It just takes an array of values for new row. Thus, orders of values here plays the very important role. There is no special validation and checkings of the passed arrays. So you must be sure what you are passing there.
For these reasons, we make this method as internal. You can use it when import proper header file like shown in code snippet below.
The benefit in using this method is in its simplicity and conciseness.
#import |