NG ConnectionPack Guide


State-less and Memory Management


NG ConnectionPack components implements state-less wrappers for corresponding REST services. Components do not store internally any returned data, such as file objects or calendar events.

 

This implies, that on each request new set of data will be returned. Most of such data types are really implemented as Delphi records (thanks to Delphi new language features, which allow the records to have properties and methods). However, NGObject data values are real Delphi objects and some of them need to be destroyed manually.

 

For example, to to retrieve file list from the Google Drive service the following code can be used:

 

var
  lst: TNGFileList;
  fl:  TNGFile;
  i:   Integer;
begin
  lst := NGGDrive1.ListFiles
                  .Q('trashed=false')
                  .Execute;
  try
    for i := 0 to lst.Files.High do
    begin
      fl := lst.Files[i];
      Memo1.Lines.Add(fl.Name);
    end;
  finally
    lst.Free;
  end;
end;

 

Objects lifetime

 

Instances of NGObject base class descendants are real Delphi objects, and thus, need to be destroyed eventually. The data of rest operation request and returned result is organizes as a tree (JSON is really a tree structure), where high-level object contain child objects directly as values of object properties or indirectly as a children of children or items of lists and maps. High-level object always owns all its child objects, and this, child objects should not be destroyed manually. Also, the user should not use manually created object in more than one place in that data tree, because then, it will be destroyed more than once; for each object property, which needs to have not null value, new unique object instance should be created.

 

Moreover, request data itself is implemented as a Delphi smart-record, and so, the user should never try to destroy request or any of its child objects manually.

 

However, rest operation result data is usually an object, which is real Delphi class instance, and so, it should be destroyed manually, after all required data has been read by the application.