Recently, I try to use MVP
Pattern in my android project.
I know that I can't do bussiness logic job in View
layer. View
must deliver all works to Presenter
layer then wait for the result from Presenter
.
View
should do anything in UI thread. But Presenter
may do something in Sub-Thread
.
How do I control MultiThreading in View
layout and Preseneter
layer?
Any help will be highly appreciated.
You have two directions you need to communicate:
a) non-UI to UI thread
and
b) UI thread to non-UI thread.
For the first case, a popular way these days is to use runOnUiThread()
Here's a nice survey of this technique and other popular options:
http://www.intertech.com/Blog/android-non-ui-to-ui-thread-communications-part-1-of-5/
As for the other direction, it is not typically necessary unless you have slow blocking operations. It is more a question of clean organization of your source code. A modern technique for sending results back from View to Presenter is using custom interfaces in Fragments like this in the "Communicating with the Activity" section:
http://developer.android.com/guide/components/fragments.html
Note that you should not put long running operations on the UI thread because it will make your app feel sluggish. Try to run them on a different thread if they will take more than 50 milliseconds or so.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With