简体   繁体   English

等待调用AsyncTask,直到上一个完成为止

[英]Wait to call AsyncTask until a previous one finishes

I have an application on an android tablet that sends a log file back to me after the user does certain things. 我在Android平板电脑上有一个应用程序,该应用程序在用户执行某些操作后将日志文件发送回给我。 I have had it setup already, being able to send the log file that I am creating in an AsyncTask. 我已经安装了它,能够发送在AsyncTask中创建的日志文件。 As the log file gets bigger and bigger it takes longer to send, and I am starting to run into an issue where it starts sending another log file before the first one finishes. 随着日志文件变得越来越大,需要花费更长的时间发送,而且我开始遇到一个问题,即在第一个日志文件完成之前,它开始发送另一个日志文件。

The code is simply 代码很简单

private class SendLogFile extends AsyncTask<Void, Void, Void> {
    @Override
    protected Void doInBackground(Void... params) {
        //Send Log File From Tablet to Server
    }
}

being called with a simple 被一个简单的调用

new SendLogFile().execute();

Is there a way for me to not run a new SendLogFile AsyncTask if there is one currently executing? 如果当前正在执行一个新方法,是否可以不运行新的SendLogFile AsyncTask? I do NOT want to set it up as a recurring task with a timer, I still want it started off user actions. 我不想将其设置为带有计时器的重复任务,我仍然希望它从用户操作开始。

Thanks. 谢谢。

You realize that you're creating a bottleneck here, right? 您意识到您正在这里创建瓶颈,对吗? If you have trouble as the log file gets bigger, your approach is wrong. 如果在日志文件变大时遇到麻烦,则您的方法是错误的。 Don't send up the whole log, send up the diffs. 不要发送整个日志,发送差异。

Ignoring that- I wouldn't use an AsyncTask. 忽略这一点-我不会使用AsyncTask。 I'd use a Thread. 我会使用线程。 The Thread sits around and waits for a send log message. 线程四处闲坐,等待发送日志消息。 When it gets one it sends it. 当它得到一个它发送它。 Since its the only thread sending logs it can never be sending two at once. 由于它是发送日志的唯一线程,因此永远无法一次发送两个日志。

You can use a ThreadPoolExecutor with a size of 1, and just submit tasks to it. 您可以使用大小为1的ThreadPoolExecutor ,只需向其提交任务即可。 It will only execute one after the other. 它只会一个接一个地执行。

调用newSingleThreadExecutor()并在返回的执行程序上运行AsyncTaskhttps : //developer.android.com/reference/java/util/concurrent/Executors.html#newSingleThreadExecutor()

There is a simple solution for this problem, use Intent services. 有一个简单的解决方案,使用Intent服务。 You can execute as many as you want and it will automatically create a qeue and execute it one after another until there are no commands left, that will solve your problem, use this class that i created. 您可以执行任意数量的操作,它将自动创建一个qeue并一个接一个地执行它,直到没有剩余命令为止,使用我创建的此类可以解决您的问题。

public class FeedBackSyncCommand extends IntentService {

    public static final String ARG_LOG_FILE = "Log";

    /**
     * Creates an IntentService.  Invoked by your subclass's constructor.
     *
     * @param name Used to name the worker thread, important only for debugging.
     */
    public FeedBackSyncCommand(String name) {
        super(name);
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        String logFile = intent != null ?
                intent.getStringExtra(ARG_LOG_FILE) :
                "Log not available";

        // do stuff with your log file
    }
}

And you can use it this way (starting 10 instances of the service): 您可以通过以下方式使用它(启动10个服务实例):

Intent feedBackCommand = 
                new Intent(getApplicationContext(), FeedBackSyncCommand.class);

    for (int i = 0; i < 11; i ++) {
        feedBackCommand.removeExtra(FeedBackSyncCommand.ARG_LOG_FILE);
        feedBackCommand.putExtra(FeedBackSyncCommand.ARG_LOG_FILE, 
                "This is an example log file" + 1);
        getApplicationContext().startService(feedBackCommand);
    }

I suggested to use RxJava if it is possible, since the logic flow is more advanced and complicated in the future. 我建议尽可能使用RxJava,因为将来逻辑流程会更高级和更复杂。 And more separate thread is needed for the job. 并且需要更多单独的线程来完成这项工作。 I think you should include some logic in Fragment/Activity for you work (They are in the zone between View and controller in MVC Pattern) 我认为您应该在工作中在“片段/活动”中包含一些逻辑(它们位于MVC模式中“视图”和“控制器”之间的区域)

From Gabe Sechan's response, it would be great if you can just send the diff to the server. 根据Gabe Sechan的回应,如果您可以将差异发送到服务器,那将是很好的。 If it is not possible, you can to save the current checksum of the file in your device. 如果不可能,则可以将文件的当前校验和保存在设备中。 At least it can save you the time to upload if there is no change in the log. 如果日志没有变化,至少可以节省您的上载时间。

Tutorial is here for replacing asynctask. 此处是用于替换asynctask的教程。 http://blog.stablekernel.com/replace-asynctask-asynctaskloader-rx-observable-rxjava-android-patterns/ http://blog.stablekernel.com/replace-asynctask-asynctaskloader-rx-observable-rxjava-android-patterns/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM