简体   繁体   English

同步多个AsyncTask数据以完成

[英]Synching Data Multiple AsyncTask to complete

i have two asynctasks. 我有两个asynctasks。 One asynctask is called and the other is called in the onPostExecute of the first one. 一个异步任务被调用,另一个异步任务在第一个的onPostExecute中被调用。 These tasks happened when the user clicks a button. 这些任务在用户单击按钮时发生。 The problem is task one is started when the user clicks the button fast and didn't wait for the second asynctask to complete yet. 问题是当用户快速单击按钮并且不等待第二个异步任务完成时,任务一开始。

Task 1 -> insert into a column, onPostExecute starts Task 2 -> increment column to insert 任务1->插入列中,onPostExecute启动任务2->增量列以插入

What ended up happening when users click too fast is that it runs Task 1 -> Task 1 -> Task 2 -> Task 2 This resulted in both column insert mapping onto the same column before the columns even incremented (task 2). 当用户单击速度过快时,最终发生的事情是它运行任务1->任务1->任务2->任务2,这导致两个列插入映射到同一列之前,甚至没有增加列(任务2)。

How do i fix this? 我该如何解决? In a way, i want it to be real time, and it should work with multiple users. 在某种程度上,我希望它是实时的,并且应该与多个用户一起使用。 I have two different SQL queries, one to update value in a column and one to update current column so next insert is in the next column. 我有两个不同的SQL查询,一个用于更新一列中的值,另一个用于更新当前列,因此下一个插入在下一列中。 I use MYSQL. 我使用MYSQL。

Thanks. 谢谢。

sounds like you should be using transaction here. 听起来您应该在这里使用交易。 See: Using Transactions 请参阅: 使用事务

if you don't want to use transaction, another option would be to do a producer/consumer using semaphores. 如果您不想使用事务,另一种选择是使用信号量进行生产者/消费者。 Initalize semaphore with 1 token. 用1个令牌初始化信号量。 Task 1 would first acquire a semaphore. 任务1首先会获得一个信号量。 at the end of task 2, task 2 would release a semaphore. 在任务2结束时,任务2将释放一个信号量。 Therefore you are ensuring that only 1 chain can be processing at a time. 因此,您确保一次只能处理1个链。

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

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