VirtualStringTree Correct/recommended use

怎甘沉沦 提交于 2019-12-03 16:34:23

The reason why AddChild() is not encouraged is that it breaks the virtual paradigm of the component - you create all the nodes, even though you may never need them. Say the query returns 100 records but the tree shows 10 nodes at the time. Now if the user nevers scrolls down you have wasted resources for 90 nodes as they are never needed (don't became visible). So if you're worried about memory usage, AddChild() is bad idea. Another thing is that if the resultset is big, populating the tree takes time and your app isn't responsive at that time. When using virtual way (RootNodeCount and OnInitNode) the tree is "ready" immediately and user doesn't experience any delay.

Then again, in case of (relatively) small resultsets using AddChild() might be the best option - it would allow you to load data in one short transaction. Ie in case of tree structure it would make sense to load whole "level" at once when user expands the parent node.

Using DB with VT is a bit tricky as the DB query is special resource too, with it's own constraints (you want to keep transactions short, it is relatively slow, the DB might only support one way cursor etc).
So I'd say it is something you have to decide on each individual case, depending on the use case and amount of data, ie

  • small resultset is OK to load all at once with AddChild() or into internal data structure and then using it throught VT's events;
  • loading one level at time is probably good compromise for trees;
  • in case of very large resultsets loading in batches might give good perfomance and memory usage compromise but adds complexity to the code managing VT.
user1089359

TQuery provide array-like access to records.

If you check with the documentation you can jump to each record using the RecNo property and you can jump to each field using Fields[i] (by it's index).

There is no holding back using TVirtualStringTree as virtual mode connecting with database.

Once you execute the query the data is available in memory as TDataSet anyway so why don't use it as a data container?

PS: Accessing fields using their index is faster than FieldByName.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!