alexrintt / shared-storage

Flutter plugin to work with Android external storage.
http://alexrintt.io/shared-storage/
MIT License
52 stars 26 forks source link

Can't call multiple 'listFiles' at once #120

Open Tamerlanchiques opened 1 year ago

Tamerlanchiques commented 1 year ago

Wanted to get count of files of each persisted folder that means I want to call 'listFiles' multiple times depending on persisted folders count. But I get only last folder item count if not "awaiting" for each function call in loop.

https://user-images.githubusercontent.com/11006352/230513106-5ead259c-bc5c-4ffe-bbd6-e609d2797ae1.mp4

Quick demo, where I perfom 'listFiles' with "await" and not

Looks like that function acts not like it intended as I can't see any notes that I can't perform it multiple times at the same time.

Also… maybe there are more efficient way to get persisted folders items count than use of 'listFiles'?

alexrintt commented 1 year ago

This happens because of Flutter EventChannel implementation. Event channel in Flutter can handle 1 stream request at once, when a new request is made the previous one gets canceled. Unfortunately I got aware of this limitation only when I developed another plugin years later that required multiple event channel calls.

This means that we have no easy-peasy solution. We'll probably need to re-write the entire API signature (or create a new one and deprecate listFiles) to full-fill this use-case.

The first solution that comes to my mind is to use MethodChannels instead and do this hacky to assign an ID to each request.

Unfortunately I won't have time anytime soon to implement it, but feel free to send a PR if you are comfortable with Kotlin and Android.

alexrintt commented 1 year ago

maybe there are more efficient way to get persisted folders items count than use of 'listFiles'?

The library SimpleStore for native Android is also using listFiles API, exactly here 1 and here 2. I learned a lot of SAF hacks using this library as base and seems they were not able to find another way to get the "files count" property. So I don't think there is at all.

SAF is garbage when we talk about performance. Their findFile implementation is just brute for loop through all files, can't believe Google published this as being part of the official Android SDK.