Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to control MultiThreading in MVP pattern for Android development?

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.

like image 708
user4470672 Avatar asked Oct 31 '22 00:10

user4470672


1 Answers

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.

like image 89
Rudi Cilibrasi Avatar answered Nov 08 '22 04:11

Rudi Cilibrasi