TStringList and TFDMemTable Contribute To The Powerhouse That Is Cross Platform Delphi Development In FireMonkey

FireWind

Свой
Регистрация
2 Дек 2005
Сообщения
1,957
Реакции
1,199
Credits
4,009
TStringList and TFDMemTable Contribute To The Powerhouse That Is Cross Platform Delphi Development In FireMonkey
July 26, 2020
[SHOWTOGROUPS=4,20,22]
One of my all time favorite objects in Delphi is TStringList which I have been using for over 20 years. Most apps I write with Delphi usually contain at least one usage of TStringList. I rarely use arrays as TStringList usually fills that space in my toolchest (the Для просмотра ссылки Войди или Зарегистрируйся makes them tempting though). TFDMemTable is becoming the new TStringList for me though as now I rarely write an app that doesn’t use a TFDMemTable at least once. Yet TStringList is still use at least once as well!

TStringList is such a versatile object because it supports easily returning it’s contents as a string and saving/loading those same contents from a file. It easily handles key value pairs, splitting strings, and holding whatever string list (as the name implies) in memory. It handles sorting and even custom sorting. It can hold a list of objects that are tied to a string name and it can even free those objects automatically with the OwnsObjects property. It can ignore duplicates like an index database table. It can even be easily searched using IndexOf or Find.

Some of my favorite TStringList methods are TStringList.SaveToFile(), TStringList.LoadFromFile(), TStringList.SaveToStream(), TStringList.LoadFromStream(), TStringList.Append(), TStringList.AddObject(), TStringList.AddStrings(), and TStringList.IndexOfName(). While the properties I use most are probably TStringList.Text, TStringList.ValueFromIndex, and TStringList.Values. You can find a full breakdown of all of these methods and properties over in the Для просмотра ссылки Войди или Зарегистрируйся.


The details are lost but I think I even used TStringList and the legendary FastStrings library at the core of a search engine index I built at the dawn of the new millennium with ~100,000 records. The results were returned in a second or two.

[/SHOWTOGROUPS]
 

FireWind

Свой
Регистрация
2 Дек 2005
Сообщения
1,957
Реакции
1,199
Credits
4,009
[SHOWTOGROUPS=4,20,22]
TFDMemTable Data IO

Lately I’ve been using TFDMemTable because it allows me to easily LiveBind bidirectional data to visual controls. TFDMemTable is a in memory database table that is part of the FireDAC (data access library) which is available in with FireMonkey in Delphi and C++Builder. TFDMemTable supports multiple columns (not just the 2 key value columns of TStringList), indexes, searching, exporting and importing from JSON, XML, binary, and even CSV format (through TFDBatchMove). TFDMemTable can be used as a visibility state machine for buttons through livebindings (see Для просмотра ссылки Войди или Зарегистрируйся).
TFDMemTable is also the recipient of JSON that is automatically imported and consumed from TRESTResponse and TRESTResponseDatasetAdapter. The JSON can be imported directly, based on a specific root field name, or even flatted so all of the fields appear as columns. TFDMemTable can even be setup as persistent so that it automatically loads and saves the data contained within it (see Для просмотра ссылки Войди или Зарегистрируйся) just like as if you had used SQLite.


TFDMemTable LocalSQL And Performance

I’ve easily used over 100,000 records in a TFDMemTable on the Windows platform and it was still pretty fast. TFDMemTable can be defined as a virtual table using the TFDLocalSQL component (see Для просмотра ссылки Войди или Зарегистрируйся) and then queried as if it was an SQLite table using TFDQuery and TFDTable. There are a number of performance tuning tricks for TFDMemTable available in the Для просмотра ссылки Войди или Зарегистрируйся. These tricks include turning off transaction logging, setting a record maximum, and wrapping changes in a BeginBatch EndBatch clause. When I utilize TFDMemTable with a large number of records I usually deploy the code provided in the DocWiki and see significant performance improvements.


TFDMemTable Searching

There are a number of different ways to search a TFDMemTable. Some of them require an index and some of them do not. The two main ways I’ve used are TFDMemTable.Locate() and TFDMemTable.Lookup(). The Для просмотра ссылки Войди или Зарегистрируйся function moves the cursor of the dataset to the record if it is found while the Для просмотра ссылки Войди или Зарегистрируйся function does not move the cursor to the found record and instead returns the found data. Locate() and Lookup() are not unique to TFDMemTable and can be used with most FireDAC based datasets. There is a third way to search a TFDMemTable and that is through the TFDMemTable.FindKey() method. Для просмотра ссылки Войди или Зарегистрируйся requires an index and I’ve heard it is faster than Locate() but I haven’t used it quite as much as Locate().

As we’ve seen in the above post it is really easy to work with text based files and create structured data files like JSON, XML, and CSV in Delphi FireMonkey using both TStringList and TFDMemTable. These two objects work cross platform on Android, IOS, Macos, Windows, and Linux and can really increase developer productivity through their use. Do you use TStringList and TFDMemTable? If not what do you use instead and why?
[/SHOWTOGROUPS]