Search code examples
storage-access-framework

Why does DocumentFile.getName take a relatively long time?


I have seen posts describing DocumentFile.listFiles() takes a long time. In my case, it is fairly fast but operations on the retrieved instances of DocumentFile take a long time.

DocumentFile[] aFiles = dfDir.listFiles(); //Takes 100 ms with a list of 250 files

//The following takes 5,000ms
for (DocumentFile df : aFiles) {
   boolean bFoo = df.isDirectory(); //takes about 10ms
   String sName = df.getName(); //takes about 10ms
}

Could anyone shed some light on this?


Solution

  • getName() invokes ContentResolver#query() under the hood ... [this] performs hundreds of queries, which is very inefficient."

    From an answer to the duplicate question you linked to.