Vote count:
0
an Android 4+ app should perform a long running operation. This could be copying a million files from A to B for example. To not block the UI this operation runs in the background using an AsyncTask
.
Assume that the operation needs some user feedback in the middle of the process to continue its work, e.g. "File XY already exists. Override, Irgnore or Rename?"
What is the best way to get this feedback from the user? Since the operation is running in a background thread one could not just present an AlertDialog
(or something similar) since UI interaction is only possible in the main thread...
So for I came across these solution:
- Ask for feeback before background threads starts, e.g. ask how to handle conflicts before starting to copy/move the files in the background.
- Do not handle conflicts but note them to ask the user how to handle them after the operation is complete in a new operation.
- End the background operation on the first conflict, ask the user for feedback and continue a new background operation
I do not like any of these solutions. In the first case the user is asked for feedback even if there will be no conflict at all. The second solutions is not possible if the steps have to be processed in a specific order. The third solution would result in code that is very difficult to read/understand/maintain.
A good solution would be:
- Stop the background thread
- Marshal to the UI thread and get feedback from the user
- Resume background thread and use feedback to continue the operation
Using GCD in Objectiv-C/iOS or async/await in C# this is not a big problem. But how can this be done in Android using AsyncTask
?
Aucun commentaire:
Enregistrer un commentaire