I'm trying to replicate something I did with Android studio in Delphi. In Android studio I did a project to pull down data from a REST server and use it in the GUI saving it to a local SQLite database to allow it to work off line. Android Studio made it very difficult to run queries against the database in the main thread.
The advice was to use the ORM to query the database and then map the results from the ORM's entities into domain entities. Same when doing requests to the REST server. Kotlin makes managing the lifetime of your entities easy in Aurelius the manager does that.
My idea was to create a manager in the main thread and create a new manager in each thread with OwnsObjects set to false. Then add the objects to the main thread manager so that I could use them in the GUI and have that manager manage the lifetime of the entities.
I noted what was said has been said about initialising the MappingExplorer before using a thread. This doesn't appear to be working as I expected. I am getting some memory leaks when I close the app down from .
---------------------------
Unexpected Memory Leak
---------------------------
An unexpected memory leak has occurred. The unexpected small block leaks are:
13 - 20 bytes: Unknown x 4
21 - 28 bytes: UnicodeString x 1
29 - 36 bytes: TDriverProject x 1, UnicodeString x 2
37 - 44 bytes: UnicodeString x 4
53 - 60 bytes: TProjectItem x 2, TAssociationProxyController x 4
61 - 68 bytes: TInternalProxyInfo x 4
69 - 76 bytes: TStringList x 4
---------------------------
OK
---------------------------
I would welcome any advice how others may be approaching this problem.
Here's an example of the code I am using to get a TList of entities and use it with an Aurelius dataset in the GUI.
tprocedure TdmOrganisation.LoadProjects( OnLoaded: TProc< TList< TProject > > );
begin
if Assigned( OnLoaded ) then
begin
FreeAndNil( FProjects );
var
Explorer := TAppUtils.GetInitialisedExplorer;
TTask.Run( procedure
begin
try
var
M := TObjectManager.Create( Pool.GetPoolInterface.GetConnection, Explorer );
try
M.OwnsObjects := False;
FProjects := M.Find< TProject >.List;
TThread.Synchronize( nil, procedure
begin
for var Project in FProjects do
FManager.AddOwnership( Project );
OnLoaded( FProjects );
end
);
finally
M.Free;
end;
except
on E: Exception do
TMSLogger.ExceptionFormat( 'Load Projects: {%s},{%s}', [ E.classname, E.message ] );
end;
end
);
end;
end;