How to load a lot of files from assets asynchronously?

回眸只為那壹抹淺笑 提交于 2019-12-11 11:31:39

问题


I know that it has similar topics. But in each topic we are open InputStream before task will be run and in new thread load data. But I have 90 asset files. Is this a single way to create 90 AsyncTask? Can I get access to main thread in inBackground() method to get assets and open new InputStream? Just in my case if I'll create 90 tasks I also need to synchronize their order of launch and finish. I need a point of synchronization where my AsyncTask will be wait the main thread.


回答1:


My suggestion would be to have a Service with a ThreadPool executor and do your loading there, you can have a bunch of threads running in parallel to do your reading. When you are done you call back into your activity, you could use a broadcast, or a binder interface with a callback.

Another option is to have a fragment with setRetaininstance(true) and do it there with either AsyncTasks or a thread pool executor.

But do not do it in the activity with async tasks, if the user rotates the device you will have potentially 90 tasks running and no activity to return the results to.



来源:https://stackoverflow.com/questions/35881498/how-to-load-a-lot-of-files-from-assets-asynchronously

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