I have a background function in my app (using Ascend Tasks) which downloads stuff from the web site.
This work can be said by two different activities, and it is possible to call it twice. Therefore, background work in "Activity 1" is called "update", and continues for a while (usually takes something like 5-10 seconds). Then when this is going on, the user switches to "Activity 2" and runs "updates" again, there is a problem: an accident when both try to free up the database at the same time (Command: DELETE FROM Table), which can cause a "database lock" error or they try to duplicate the same item in the database.
I have tried to solve it by setting a stable boolean flag when a task is active. When the task is called, it will check this flag, and if it is correct (i.e. this work on other work) it goes into the waiting loop using a handler, until the flag is clear , And then returns back. To ensure that when the background function returns, it is updated. I have to use a louper for that: it sometimes fails with an error "only one thread can cause per thread" and I actually have such a way that only one loop can be started It is an offensive code, which appears at the beginning of background work:
if (active) {Looper.prepare (); Handler = new handler (); Handler.postDelayed (new runnable () {int count = 0; @ override public wad run (if (active) {count ++; if (calculation & lt; 1000) handler. PostDelayed (this, 100);}} }, 100); Looper.loop (); active = false; return "done";} And to make matters worse, it often hangs without returning to the loop.
Any ideas about solving this kind of situation?
Why not use synchronization instead? It sounds like a concatenation issue Is not it why you do not make sure that if the background work is going on earlier, then the second background job is sleeping till the end.
Or in any way to ensure that if user activity Switches to number 2, background function has been canceled from Activity Number 1.
Comments
Post a Comment