In the case of MVP, the Presenter holds a view object. While in the case of MVVM, VM does not hold View object because view updated via data binding/live data. I want to know that if I use RxJava/Data binding in the case of MVP then no need to hold view object by the presenter. Now there is any diff in MVP and MVVM? Any valuable suggestions, please?
One clear diff I can see is that in the case of MVVM, VM holds data while activity recreated. While there is no such advantage in MVP.
https://medium.com/#prajvalprabhakar/mvp-vs-mvvm-93657494106b
https://www.vogella.com/tutorials/AndroidArchitecture/article.html
i think no if we use rx with mvp than we can say its reactive programing but not exactly mvvm becuase if we see mvvm arch. it's not only include reactive things or live data its also include data biniding and many more and yes main thing it's also use repository concepts for data call (API + local), thats why we can not say that mvp with rx its same as mvvm
Related
Most of the MVVM examples are dealing with very simple user interfaces.
But lets say I have an activity with many views to update (i.e. lots of data)
As I read in other places, multiple ViewModel objects is a bad pattern.
So, as I see it there are two solutions for that:
Create a single object (and single LiveData for it), that wraps all other data objects.
But there's a problem with this - each data object that gets updated will cause the whole UI to update.
Create multiple objects (and multiple LiveData objects for it).
It means that I need to observe each LiveData object. Is there a problem with this pattern?
Thanks in Advance!
First Point you mentioned : Yes this is not optimal Pattern to do but if you have small data then, separating LiveDatas is more work for less gains
Second Point you mentioned : Yes this is more optimal, you can have a LiveData object for each View you want to update and observe them all from your activity or fragment. There are no issues in this Pattern.
About Mutilple ViewModels :
Multiple ViewModels Pattern in same Activty/Fragment is also an option if you have too many things(LiveData objects or funcitions) happening in one ViewModel. This is only recommended to make viewModels lighter. So only use this if you are having a large viewModel class
Create ViewModels for discrete types of information.
You could for example have a UserViewModel that deals with all state regarding a User. This means you can use the same ViewModel in another context, without pulling data that might not be necessary (as you would if you had a single God ViewModel).
Create as many LiveData objects as you need to model your view.
It is better to condense the data into logical objects, where possible. If only to keep things manageable.
If you have a User, you should use that for your LiveData instead of having a LiveData for E-mail address, Display name, Age, etc. That will make things much simpler for your data bindings. Try to keep things logically grouped together.
i'm trying to implement MVVM architecture + DataBinding + RxJava.
i have an activity with RecyclerView.
as i understand the ViewModel should not have a reference to the View.
what's the correct way in MVVM to pass the data list from the ViewModel to the RecyclerView in the activity?
pass the data via interface like in MVP?
You should add data binding to your ViewHolder. You can read some articles like this to make it more clear.
By the way I faced problem when using MVVM + Databinding Library when trying to apply animations or for example trying to implement postdelayed. Of course you can create tons of custom binding adapters, but it a lot easier to use view directly, so sometimes you have to use MVP like connections between ViewModel and View.
You can see my repository where I did everything using DataBinding! Visit https://github.com/xyarim/android-architecture
I'm learning MVP structure and I trying to figure out:
How to pass data between models?
Each model represents one action and if I need to send data from one
model to another one, how can I properly make it? Should I pass data
through presenter, like
firstModel -> commonPresenter -> secondModel
OR
send data between models, like
firstModel -> secondModel?
And what if these models interact with different presenters?
The Model View Presenter pattern, like most architectural patterns, is quite open to experimentation. The important thing to keep in mind is to separate the View from the domain logic and data handling.
Particularly in Android, it's useful to keep the code as far as possible from Framework specific classes like Activities and Fragments.
In my experience, it's best to let Models communicate amongst themselves, as Observers of each other. The same goes for Views and Presenters, or, in general, any component in the same architectural layer.
AFAIK you have to convert your Domain models to your Ui models in presenter so you have to call a method or a constructor like this in presenter:
ModelOne modelOne = new ModelOne(modelTwo);
please consider DRY principals and do NOT assign each field in presenter itself like this
modelOne.title = modelTwo.title;
modelOne.id = modelTwo.id;
but if you want convert a Domain layer model to another Domain layer model you have to do it in other layers. read this for more info:
http://fernandocejas.com/2014/09/03/architecting-android-the-clean-way/
"the presenter communicates with model layer, converts the data to UI friendly format, and updates the view"
also you can find above sentence in this link:
http://iyadagha.com/using-mvp-ios-swift/
I'm trying to use this MVVM for my android app. I've done many apps, but I'm trying to step up on another level and trying to use the MVVM. But I need to understand where I should load the data from internet. I'm using RxJava, but I'm not sure if I should load data only in ViewModel. If so then where do I set the data. I'm using Databinding from google, but I don't wanna set data in xml through viewModel. I want to set it from the java file.
I'm sorry if I miswrote something, post an answer and I will try to fill out any required informations.
"Triggering a DataLoad" is part of presentation logic. Hence, this should belong in ViewModel.
Details about "How data is loaded" for example, networking logic, does not belong to the ViewModel layer. I highly recommend using Retrofit as you are already using RxJava.
As rx.Observableand databinding.ObservableField are very similar, you can convert them from one form to another. I have written a library that allows you to do this. See FieldUtils.java for an implementation.
Either ways, assuming you have a DataService interface/class:
public interface DataService {
Observable<String> loadSomeData();
}
you can build your ViewModel as follows:
public class ExampleViewModel {
ObservableField<String> title;
public ExampleViewModel(DataService dataService) {
this.title = FieldUtils.toField(dataService.loadSomeData());
}
}
Then, you can display this in your View using Data Binding syntax
<TextView
android:text="#{viewModel.title}" />
I recently blogged about using RxJava with MVVM. I showed an app which loads a list of events from Github using Retrofit and displays them in a RecyclerView. This has been implemented in MVVM.
Article link:
MVVM using RxJava + Data Binding example: Loading data using Retrofit
A more complicated example which also shows a loading indicator and error: DataLoadingViewModel.java.
There is two similar architectural patterns - MVP and MVVM. The main difference is that in MVP parttern Presenter desides how to display data, but in MVVM pattern View receives Model and renders itself (takes data from model). Classical MVVM example is view bindig. But the point is - nomatter what pattern you use, you should obtain all data in Model - and place all your business logic in Model too.
I try to understand what the best way is to program a "clean" Android app. I found an interesting article about it where they use a kind of a 3 layer architecture.
Now I read that android uses the MVP design pattern and try to understand this. The way I understand the MVP principle (in the android context) is that:
the Model layer are the logic classes
the presenter are the classes that are "linked" to an XML (the activities)
the passive view are the XML's.
Is this correct?
When this is correct, is it then also correct that the 3 layer model fits perfectly in the MVP principle because:
the presentation layer from the 3 layer architecture fits in the presenter layer of MVP
The business and the data layer from the 3 layer architecture fits perfectly in the model part of the MVP?
I hope my questions are clear because it is a complicated topic for me.
Maybe this clears up my vision (way of thinking) about this...
first thing I wanted to clarify is that MVP or any other pattern for that matter is no specific of Android dev and, can be applied to any other framework.
I think you got the gist of it:
view is usually implemented by activities and fragments and will contain the reference to the presenter
the presenter is that middle man between the view and model. Retrieves data from the model and returns it already formatted to the view so it doesn't need to do anything else but display it.
the model can be seen in a simplistic way as the "data provider". It can be as complex as you want, using internal db, lots of clases etc.
If you are interested in Android apps architecture I suggest you watch this talk given at Android dev summit this year. It is one of the bests I've seen
https://www.youtube.com/watch?v=BlkJzgjzL0c
Even though this question has an answer, I don't think this answer is complete by any means.
MVP is a general concept which can have many various implementations, some of which differ substantially. Moreover, the concept itself is very ambiguous - different people can have different concepts in mind when they say MVP. One of the most widespread ones is shown in the below picture:
Regardless of implementation, the general definitions of MVP components are:
Model: abstraction of "application state storage". The definition of what the "state" is and how it is stored are implementation details. Model implementations should not have dependency on View or Presenter.
View: abstraction of "user interface". The definition of who the "user" is and how it interacts with the View are implementation details. View implementations should not have dependency on Model or Presenter.
Presenter: encapsulates application's business logic. Presenter processes user input events received from view, and alters application's state stored in model in response. Presenter also processes changes of application's state stored in model and updates view in response. Presenter usually depends on both the View and the Model.
If you need more information about MVP in context of Android development, you can find it in this post: MVP and MVC Architectural Patterns in Android
Important issues which need to be addressed while implementing MVP in android are activity leaks which cause memory leaks and app crashes due to background process updating closed activity.
Due to presenter having reference to activity, if presenter can't be garbage collected, activity will stay in memory.
Both of the issues can be solved by using life cycle methods of activity or fragment and releasing resources in those methods so that memory leaks and app crashes are prevented.
Cleaning up of resources related background work can be easily implemented using RXJava, for more information about MVP and MVP with RXJava, see http://www.zoftino.com/android-model-view-presenter-mvp-pattern-example
Here is simplest way to implement MVP pattern in your application android_mvp_login_sample
As you have come to know basics of Clean Architechure. The following example depicts how actual your MVP pattern is implemented.
Example:
interface BaseContract {
interface BaseView {
//Methods for View
void onDoSomething();
}
interface BasePresenter {
void doSomething();
}
}
class BaseMainPresenter implements BaseContract.BasePresenter {
BaseContract.BaseView view;
BaseMainPresenter(BaseContract.BaseView view) {
this.view = view;
}
#Override
public void doSomething() {
if (view != null)
view.onDoSomething();
}
}
class DemoClass implements BaseContract.BaseView {
//Create object of Presenter
/****
* Example :
* BaseMainPresenter baseMainPresenter = new BaseMainPresenter(this);
*/
#Override
public void onDoSomething() {
//Deal with Context here.
}
}
Refer below link for sample Actual implementation with scenario & learn more about Clean Architechure : https://github.com/android10/Android-CleanArchitecture
Here on github
https://github.com/saksham24/Android-Firebase-Mvp-Mvc-Mvvm-chat
i made a repo containing 3 applications with same functionality but written in 3 different android patterns(Mvc, Mvp, Mvvm)
Understanding three different pattern is quite easy if we get a simple good example on them so i made a repo to contribute my knowledge to this developer community.
Also the repository is written using proper java guidelines and conventions(including naming and packages, modules) so people looking for such project can also view this repository.
now
if you want to know the difference between android Mvp,Mvc, MvvM see this explanation by realm
https://academy.realm.io/posts/eric-maxwell-mvc-mvp-and-mvvm-on-android/
if you want to compare three pattern see this wonder full blog https://thinkmobiles.com/blog/mvp-vs-mvvm-android-patterns/
Now I read that android uses the MVP design pattern and try to
understand this. The way I understand the MVP principle (in the
android context) is that:
the Model layer are the logic classes
the presenter are the classes that are "linked" to an XML (the activities)
the passive view are the
XML's.
Is this correct?
Not fully: for the Model layer it is true, but for the Presenter it is not. The Presenter is not linked to XML although it has reference to the View through its constructor. The View is the Activity/Fragment in android.
You might want to check here for a sample MVP app for android.