Reputation: 32243
When I want to do something in background, if the action is very simple: Like do "something in background" and then update the UI, instead of using an AsyncTask
I'm considering to use (just for faster coding):
new Thread(){
public void run(){
final ArrayList<myObjects> objects= myDatabase.queryObjects();
runOnUiThread(new Runnable() {
@Override
public void run() {
updateUIWith(objects);
}
});
}
}
But I really don't know if using "final" objects that way can result in memory leaks or have other kind of troubles.
Is using this method OK?
Upvotes: 1
Views: 547
Reputation: 22389
The question is: Is it a good idea to open a new thread for every little action? AsyncTask provides the convenience of the threads being managed by someone else, but when you declare a thread like you did, you're responsible for dealing with situation such as limitations on the number of threads.
Upvotes: 0
Reputation: 328737
final
only says to the compiler that you won't reallocate the objects
variable in your code. There is no link between final
and memory leaks.
If you use an anonymous inner class (the new Runnable...
) you have to make objects
final.
I am not very familiar with Android but if your updateUIWith()
method does interact with UI objects, it might be an issue as I would assume UI updates need to run in a specific UI thread. If you don't update UI objects in that method then you code should be fine.
Upvotes: 3