Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Live Data in MVP architectural pattern

I have an application with MVP architectural pattern. Now, I am trying to implement Livedata in my application. I searched a lot , but I can not found any tutorial or example for this. All tutorial says live data is for MVVM pattern.

Applying Livedata in MVP pattern is a correct method or not?

If anyone have idea about implementing Livedata in MVP please share.

Thanks in advance.

like image 395
Ranjith KP Avatar asked Jun 21 '18 00:06

Ranjith KP


People also ask

Can we use live data in MVP?

Using livedata instead of MVPview in MVP makes the presenter loosely coupled with the view and helps in reusing presenter. We can create different presenter for different components and expose events using livedata, it also encourages reactive architecure.

What is MVP architecture pattern?

MVP (Model — View — Presenter) architecture is one of the most popular architecture patterns and is valid in organizing the project. MVP (Model — View — Presenter) comes into the picture as an alternative to the traditional MVC (Model — View — Controller) architecture pattern.

What are the differences between MVP and MVVM architectural patterns?

Difference Between MVP and MVVM Design PatternMultiple View can be mapped with single ViewModel. The Presenter has knowledge about the View. ViewModel has no reference to the View. Model layer returns the response of the user's input to the Presenter which forwards it to View.

Is MVP better than MVVM?

MVVM is better than MVC/MVP because of its unidirectional data and dependency flow. Dependency is one way, thus it is a lot easier to decouple it when we need to. It is also easier for testing. All my projects(written in Kotlin for Android app) are based on MVVM.


1 Answers

Lifecycle aware MVP is a good solution.

As you know Architecture Components introduce LiveData, a lifecycle-aware observable data holder class - the idea is great and sounds super handy. AC causes some "additional code to handle null case in our onChanged implementations" and also it's "harder to read and understand the flow" in the code.

Lifecycle aware MVP solve mentioned issues with these benefits:

  1. solve the lifecycle and configuration changes problems,
  2. clear and explicit View actions, like in classic MVP
  3. no LiveData and no Resource’s state handling in Activities or Fragments
  4. it’s easier to pass any necessary parameters straight to view methods (directly communicating with the view)

so if you have an existing MVP architecture and want to make use of ViewModels and their handling of configuration changes, this is an easy way to do achieve it

you can find more about it here.

like image 104
majid ghafouri Avatar answered Oct 13 '22 00:10

majid ghafouri