如何异步从资产加载很多文件? [英] How to load a lot of files from assets asynchronously?

查看:78
本文介绍了如何异步从资产加载很多文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道它有类似的主题.但是在每个主题中,我们都打开InputStream,然后再运行任务,并以新的线程加载数据.但是我有90个资产文件.这是创建90 AsyncTask的单一方法吗?我可以使用inBackground()方法访问主线程以获取资产并打开新的InputStream吗?就我而言,如果我要创建90个任务,我还需要同步它们的启动和完成顺序.我需要一个同步点,其中AsyncTask将等待主线程.

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.

推荐答案

我的建议是拥有一个带有ThreadPool执行器的服务,然后在其中进行加载,您可以让一堆线程并行运行以进行阅读.完成后,您可以回调到活动中,可以使用广播或带有回调的活页夹接口.

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.

另一种选择是使用setRetaininstance(true)创建一个片段,并使用AsyncTasks或线程池执行程序在其中执行.

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

但是不要在带有异步任务的活动中执行此操作,如果用户旋转设备,则可能正在运行90个任务,并且没有活动可将结果返回到该活动.

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.

这篇关于如何异步从资产加载很多文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆