PDA

View Full Version : Safe to store QFileInfo objects?



Escot
17th May 2017, 17:02
I came across a post a while back saying that you shouldn't store QFileInfo objects. I can't find the post but I seem to recall that the reason given was related to the caching feature. Reading the QFileInfo documentation it doesn't appear say that QFileInfo objects should not be stored, so I'm not sure if this information was correct.

I wanted to store information on a large number of files, and was going to store the QFileInfo objects for each file. However, I'm now cornered that this is the wrong thing to do and storing a large number of QFileInfo objects may cause performance issues.

Is it safe to store QFileInfo objects or would it be better to extract the information I need to a separate class and store that?

d_stranz
18th May 2017, 06:14
I don't think there is a problem with storing QFileInfo instances. The issues would arise when you read them back in and want to use them. In such a case, I would read it in, then call QFileInfo::refresh() to force a re-caching from the file system, then call QFileInfo::exists() to verify that it still references a valid file.

But I think it would be far easier to simply store the absolute path of the file, then read that back in to create the QFileInfo instance. You would call QFileInfo::exists() in that case as well. Everything you need from QFileInfo is embodied in the absolute path, since that is usually the way you create them in the first place.

Escot
18th May 2017, 14:26
The issue you discuss of the cache becoming out of date is probably what that other post was referencing when it recommended not storing QFileInfo objects.

I was intending to use a QFileSystemWatcher to ensure that the QFileInfo objects were kept up to date, so it sounds like storing the QFileInfo objects should be fine.

Thanks a lot for your advice.

d_stranz
18th May 2017, 17:43
I was intending to use a QFileSystemWatcher to ensure that the QFileInfo objects were kept up to date, so it sounds like storing the QFileInfo objects should be fine.

Then I have no idea what you mean by "storing". I assumed you meant saving persistently in a file, in which case using a QFileSystemWatcher makes no sense.