Newbie Question about TObjectManager

I am trying to learn and catch up with Aurelius concepts.

I am somewhat confused about WHEN to use TObjectManager**.Clear**, and WHEN to use TObjectManager.Flush

For Update/Save, I should call Flush, but for Delete, I shouldn't ?

Also, in what situation I must call TObjectManager.Clear?

Flush is the command that perform updates in the database. So Delete for delete, Flush to update. Flush also include/exclude itens from lists - in other words, set the foreign key value of child records.

Clear performs just in-memory operation, it doesn't have to do with the database. Clear will remove and destroy all objects inside the manager cache. You might want to call Clear when you think you might have too many objects in the manager that you don't need anymore. There aren't many situations you might need to call Clear.

1 Like

Thank you. Just to confirm - If I don't call "Flush", all changes (Add, Update, or Delete) will remain in-memory, and will NOT be send to the back-end database, right?

Flush is needed to send object modifications to the database - eventually, it will perform UPDATE SQL statements. So when you modify property values, call Flush to update them in the database.

You don't need to call Flush after you call Save (which inserts a record) or Delete (which deletes a record).

I don't see any reference to Flush in the link you provided?

1 Like

Hi Wagner,

Thank you again for the clarification. Sorry I meant this link (Aurelius and MVVM Part 2). http://www.kouraklis.com/2017/01/tms-aurelius-and-mvvm-design-an-example-part-2/

In the following code, John called "FObjectManager.Flush" AFTER "FObjectManager.Save", which is NOT necessary?

procedure TModelMain.addCustomer(var aCustomer: TCustomers);
begin
  if Assigned(aCustomer) then
  begin
    fObjectManager.Save(aCustomer);
    fObjectManager.Flush;  // <------  Is this necessary?
  end;
end;
 
procedure TModelMain.addOrUpdate(var aCustomer: TCustomers); 
begin 
  if Assigned(aCustomer) then 
  begin 
    fObjectManager.SaveOrUpdate(aCustomer); 
    fObjectManager.Flush; 
  end; 
end;

procedure TModelMain.deleteCustomer(var aCustomer: TCustomers);
begin
  if Assigned(aCustomer) then
    fObjectManager.Remove(aCustomer);
end;

procedure TModelMain.updateCustomer(var aCustomer: TCustomers);
begin
  if Assigned(aCustomer) then
  begin
    fObjectManager.Update(aCustomer);
    fObjectManager.Flush;
  end;
end;

procedure TModelMain.getCustomerList(var aList: TObjectList);
begin
  fObjectManager.Clear;
  aList:=fObjectManager.Find.OrderBy('LastName').List;
end;
 

Indeed, that Flush call after Save is not necessary.

This topic was automatically closed 60 minutes after the last reply. New replies are no longer allowed.