@
Tuğrul HELVACI çok haklı olarak, çok ama çok önemli hatama dikkatimi çekti.
Acele ile yazınca oluyor böyle hatalar ama bu fahiş hata sayılanlardan, yani özürü yok
MemoryLeak bir projede en kaçınılması gereken hatalardan biridir. Buradaki örnekte OnCreate olayına konacak kısmı Button altına koyunca her butona basışta var olan nesneye bir yenisi eklenerek şişiyor olduğunu fark etmemişim.
Ayrıca Thread içerisinde aynı connection'u kullanmışım, ona da bir ek connection create edilmesi sağlandı.
Kaynak kodun üzerini çizerek yenisini yolluyorum.
Uses System.IOUtils,
System.Notification,
Data.DB,
FireDAC.Stan.Intf,
FireDAC.Comp.UI,
FireDAC.FMXUI.Wait, {FDGUIxWaitCursor.Provider := 'FMX'}
FireDAC.DApt,
FireDAC.Comp.Client,
FireDAC.Stan.Param,
FireDAC.Stan.Def,
FireDAC.Stan.Async,
FireDAC.Phys.Intf,
FireDAC.Phys.SQLiteWrapper.Stat,
FireDAC.Phys.SQLite;
Var
FDGUIxWaitCursor : TFDGUIxWaitCursor;
FDPhysSQLiteDriverLink : TFDPhysSQLiteDriverLink;
FDGUIxAsyncExecuteDialog : TFDGUIxAsyncExecuteDialog;
FDBFileName : String;
procedure TForm1.DB_Prepare( dbName: String = 'dbSqlite.db' );
Var
LConnection : TFDConnection;
LQuery : TFDQuery;
LRes : TResourceStream;
begin
{$if Defined(MSWINDOWS)}
FDBFileName := System.IOUtils.TPath.Combine( TPath.Combine(GetCurrentDir, 'DATA'), dbName);
{$elseif Defined(ANDROID)}
FDBFileName := TPath.Combine( TPath.Combine( TPath.GetDocumentsPath, 'DATA'), dbname);
{$endif}
if NOT DirectoryExists ( ExtractFilePath(FDBFileName))
then ForceDirectories( ExtractFilePath(FDBFileName) );
aNotif( 'DB_Background', 'DB Path', FDBFileName );
if NOT FileExists( FDBFileName ) then
begin
LConnection := TFDConnection.Create( nil );
LConnection.Params.Values['DriverID'] := 'SQLite';
LConnection.Params.Values['database'] := FDBFileName;
LQuery := TFDQuery.Create( nil );
try
LQuery.Connection := LConnection;
LQuery.SQL.Text := ''
+ 'CREATE TABLE ''OrnekTable'' ('
+ ' ''id'' INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,'
+ ' ''Ad'' varchar(255) default NULL,'
+ ' ''DogTar'' varchar(255),'
+ ' ''Adres'' varchar(255) default NULL,'
+ ' ''Telefon'' varchar(100) default NULL )'
;
try
LQuery.ExecSQL;
except
aNotif( 'DB_Background', 'DB Cretation', 'CREATE Edilemedi...' );
Exit;
end;
LRes := TResourceStream.Create( HInstance, 'SampleData', RT_RCDATA );
try
LQuery.SQL.LoadFromStream( LRes );
LQuery.ExecSQL;
finally
FreeAndNil(LRes);
end;
LConnection.Commit;
finally
FreeAndNil(LQuery);
FreeAndNil(LConnection);
end;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
DB_Prepare();
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
TThread.CreateAnonymousThread(procedure ()
var
LQuery : TFDQuery;
LConnection : TFDConnection;
begin
LConnection := TFDConnection.Create(nil);
LQuery := TFDQuery.Create( nil );
try
LQuery.Connection := LConnection;
LConnection.Params.Values['DriverID'] := 'SQLite';
LConnection.Params.Values['database'] := FDBFileName;
LQuery.SQL.Text := 'SELECT * FROM ''OrnekTable'' ';
LQuery.Active := True;
TThread.Synchronize (TThread.CurrentThread,
procedure ()
begin
aNotif( 'DB_Background', 'Rescord Count', InttoStr(LQuery.RecordCount) );
end);
while NOT LQuery.Eof do
begin
TThread.Synchronize (TThread.CurrentThread,
procedure ()
begin
ListView1.Items.Add.Text := Format( '%s : %s', [ LQuery.FieldByName('Ad').AsString, LQuery.FieldByName('DogTar').AsString ] );
end);
LQuery.Next;
end;
LQuery.Active := False;
finally
FreeAndNil(LQuery);
FreeAndNil(LConnection);
end;
TThread.Synchronize (TThread.CurrentThread,
procedure ()
begin
aNotif( 'DB_Background', 'DB Background', 'DB ListView aktarma bitti...' );
end);
end).Start;
end;
procedure TForm1.aNotif( aName, aTitle, aBody : String );
var
LNotification: System.Notification.TNotification;
begin
With System.Notification.TNotificationCenter.Create(nil) do
try
if Supported then begin
LNotification := CreateNotification;
try
LNotification.Name := aName;
LNotification.Title := aTitle;
LNotification.AlertBody := aBody;
PresentNotification(LNotification);
finally
LNotification.Free;
end;
end;
finally
Free;
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
ReportMemoryLeaksOnShutdown := true;
FDGUIxWaitCursor := TFDGUIxWaitCursor.Create(nil);
FDPhysSQLiteDriverLink := TFDPhysSQLiteDriverLink.Create(nil);
FDGUIxAsyncExecuteDialog := TFDGUIxAsyncExecuteDialog.Create(nil);
FDPhysSQLiteDriverLink.DriverID := 'SQLite';
FDGUIxWaitCursor.Provider := 'FMX';
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
FreeAndNil(FDGUIxWaitCursor);
FreeAndNil(FDPhysSQLiteDriverLink);
FreeAndNil(FDGUIxAsyncExecuteDialog);
end;