ObjectDataSet - deletion failure on sorted list

Issue #414 resolved
Adam Siwoń created an issue

Hi,

I found the problem with deletion record from ObjectDataSet, when the list assigned to the ObjectDataSet is sorted. I know it is not an usual case, but sometimes happens. The problem occurs because ObjectDataSet stores wrong index to the data list items. Very similar problem was reported in issue #215. Rainer Wolff made there proposal to resolve the problem with changing sorting's methods of class TIndexList. This does not work in my case, because using sorted list causes the list changes order of items itself and after this operation index values stored in TIndexList are not valid any more.

I made review of TIndexList class code. I think the use indexes to the data list is not necessary and makes problem for sorted DataSet or list. Now indexes are used only to deletion objects from data list. The current code looks like:

procedure TIndexList.DeleteItem(index: Integer);
var
  fixIndex: Integer;
begin
  fixIndex := fIndexes[index].Index;
  fIsChanging := True;
  try
    fDataList.Delete(fixIndex);
    DeleteIndex(index);
    FixIndexes(fixIndex);
  finally
    fIsChanging := False;
  end;
end;

But I think it is not necessary. I think in fIndexes list could be stored only pointers to the DataList objects and method Remove could be used to deleting objects from DataList instead of Delete method. The DeleteItem code after changes could looks like:

    fIndexes: IList<TObject>;

function TIndexList.AddIndex(index: Integer): Integer;
begin
  Result := fIndexes.Add(fDataList[index]);
end;

procedure TIndexList.InsertItem(const item: TObject; index: Integer);
begin
  fIsChanging := True;
  try
    fIndexes.Insert(index, item);
  finally
    fIsChanging := False;
  end;
end;

procedure TIndexList.DeleteItem(index: Integer);
begin
  fIsChanging := True;
  try
    fDataList.Remove(fIndexes[index]);
    DeleteIndex(index);
  finally
    fIsChanging := False;
  end;
end;

These changes simplifies TIndexList class but deletion of records could be a little bit slower. These resolves also problem reported in issue #215. From sorted ObjectDataSet records and objects are deleted correctly.

To the issue is attached the unit Spring.Data.IndexList with changed TIndexList class. Please consider to add this change to Spring4D library.

Comments (1)

  1. Log in to comment