Model View View-Model (MVVM): Getting Started

  Переглядів 396,476

CodingWithMitch

CodingWithMitch

День тому

Watch my course on MVVM and a REST API: codingwithmitch.com/courses/r...
Personally, I think MVVM is the best way to structure code (in most situations).
The thing I like most about MVVM is the compartmentalization of the various components in a project.
1) The UI components are kept away from the business logic
2) The business logic is kept away from the database operations
3) It's easy to read (because everything has specific places to live)
4) And if done correctly, you have a lot less to worry about when it comes to lifecycle events (ex: screen rotations)
➤Blog post on MVVM:
↻ codingwithmitch.com/blog/gett...
➤My Courses
↻ codingwithmitch.com/courses/
➤My Instagram:
↻ / mitch.tabian
➤Merchandise
↻ store.codingwithmitch.com/

КОМЕНТАРІ: 366
@BehrinHalilov
@BehrinHalilov 5 років тому
You can make few optimizations : 1. Optimize the layouts - decrease the layout nesting as much as possible to increase the performance. 2. Remove one dependency (circle image view) since Glide provides an options to transform an image into circular one. 3. Avoid non-static or anonymous instances of AsyncTask - leads to memory leak. 4. Instead of NotifyDataSetChanged that will refresh the whole collection. You can just notify an item position or range that you are updating (NotifyItemInserted or NotifyItemRangeChanged). This will cause less ui calculations and drawing. Cheers!
@babbintandukar9959
@babbintandukar9959 5 років тому
wow didn't knew there was notifyiteminserted or notifyitemchanged till now been almost 8 months in android thanks man
@arpitkumar4525
@arpitkumar4525 4 роки тому
Any sources/references on how to use Glide to transform an image into a circular one?
@ritvijsaxena7201
@ritvijsaxena7201 4 роки тому
For AsyncTask, it's better to give weak reference using interfaces rather than using non-static instances
@sanaebadi97
@sanaebadi97 4 роки тому
@@arpitkumar4525 Glide.with(context) .load(url) .apply(RequestOptions.circleCropTransform()) .into(imageView);
@maxmustermann7954
@maxmustermann7954 4 роки тому
@@ritvijsaxena7201 How would one achieve point 3)? Even the JSONObjectRequest class from Android uses anonymous instances/lambdas.
@paweguszczak8631
@paweguszczak8631 5 років тому
Each time when I'm thinking about learning some new good stuff - you just upload new great video about it. Keep doing your fantastic job man, your tutorials are great!
@DrSchille
@DrSchille 3 роки тому
I absolutely love your tutorials. So easy to follow and understand. I love your clean and direct explanation of the topics. Great job! Thank you so much!
@hiteshchalise3988
@hiteshchalise3988 5 років тому
Wow, just wow, I can't even begin to explain how you've inspired me. Thank you. :)
@mychannel-do1bv
@mychannel-do1bv 3 роки тому
I really like your idea to present complete examples. Many tutorials on the internet contains only fragments of the entire solution, and very often the code doesn't even compile. Very good job!
@alvaroisea2259
@alvaroisea2259 5 років тому
I'm a big fan of your tutorials man, keep up with the good work !! (Y)
@shobhitagarwal3647
@shobhitagarwal3647 5 років тому
CONGRATS ON 30K my man!
@shivamdhuria07
@shivamdhuria07 5 років тому
After trying for about months ,I think finally got this!Thank you~!
@kareemhamed5606
@kareemhamed5606 5 років тому
much appreciated i always love your work man! you are a legend.
@WingChanApp
@WingChanApp 4 роки тому
Just applied your example to my app, and it works. Thank you!
@SoyAmurita
@SoyAmurita 4 роки тому
your video is incredible, it's my first time to deal with mvvm and all problems that have faced me, you already have put the answer for them in this video. thanks much,
@dineshchoudhary8221
@dineshchoudhary8221 5 років тому
Hey Mitch, you’re doing a great job! I’m sure with time you’ll have a ton of subscribers and views.
@sudhir1274
@sudhir1274 4 роки тому
Yet another high quality tutorial from you Mitch. I could not understand how MVVM works. The other tutorials go into details of databases and related topics. The result is MVVM basics are difficult to follow and implement. I did have to watch this tutorial five or six times to understand the MVVM concept. To test my understanding, the kotlin recyclerview tutorial (BlogPost) by you was used as a starting point. Following the instructions in this video and the source code for this tutorial, BlogPost source code was converted to the MVVM model used in this tutorial. Along the way I had to jump to google to solve problems when the intended kotlin code did not work as expected. The code I amended now works like this tutorial including adding new blogs by clicking the floating button. Many, many thanks for such a good tutorial. You are one of my go to channels for Android learning. Appreciate the time you take to post these tutorials.
@nicoqueijo
@nicoqueijo 5 років тому
Suggestion: enable "Show taps" on your emulator under developer options so the viewer can see exactly where you are pressing when you are demoing stuff.
@aditchauhan876
@aditchauhan876 5 років тому
Finally found an awesome Android teacher .
@safees3165
@safees3165 5 років тому
Look at that I implemented viewmodel into my app last night and this afternoon I see your video!
@PostMeridianLyf
@PostMeridianLyf 4 роки тому
This helped a lot in my understanding of MVVM. Hopefully I can build my own viewmodel soon
@chikumandal
@chikumandal 3 роки тому
watched a lots of videos for mvvm but finally this one really taught me something thank you sir awesome explanation.
@HezOmanjo
@HezOmanjo 3 роки тому
So glad I stumbled upon your tutorials! Thanks man!
@theophilus494
@theophilus494 3 роки тому
So far the only tutorial on MVVM i eventually got working... i understood it better after watching this tutorial... thank you...
@clublulu399
@clublulu399 4 роки тому
Glad I stumbled upon this. Great explanation!
@grape99902
@grape99902 4 роки тому
maaaan you just took a complicated subject and made it simple thank you!!
@terjemah_alquran
@terjemah_alquran 4 роки тому
thank you, this is awesome. really eazy to follow and focus to the important point
@user-gz1ep9sw4b
@user-gz1ep9sw4b 5 років тому
Waiting so bad for your rest api + MVVM Project!!
@zhianchen3349
@zhianchen3349 3 роки тому
Logically clear, and it helps me understand MVVM better (Also the English speech is relatively easy so I can get your point well). GOOD TUTORIAL.
@jagannathacharya8169
@jagannathacharya8169 3 роки тому
Much appreciated. This tutorial video is much informative. You have beautifully explained all the terms in MVVM. Keep it up.
@taar1
@taar1 4 роки тому
thanks for that video, it was exactly what I was looking for. Excited for MVVM now :)
@theo_st
@theo_st 2 роки тому
Thanks for the tutorial, it was really helpful! One tiny detail you could change is that, when creating classes with the singleton pattern, the getInstance method should ideally be synchronized to avoid race conditions. Apart from that, excellent tutorial! :)
@aminsoley9191
@aminsoley9191 3 роки тому
Thank you for your clear and step by step explanation !
@utkarsh5959
@utkarsh5959 5 років тому
Thanks for the demo of MVVM Liked it a lot
@omertajelsir6939
@omertajelsir6939 2 роки тому
You are creating really awsome content. We can feel that you are really putting your heart into it. I really really thank you. best of luck
@xoca7171
@xoca7171 5 років тому
Thanks man. Very clear explanation.
@skywalkerpz
@skywalkerpz 5 років тому
Great video as always, and nice teaching keep it up!
@wallacethierre9419
@wallacethierre9419 4 роки тому
Very good explanation and example, you used a simple problem to explain about live data with MVVM arch, congrats.
@nilesh6929
@nilesh6929 2 роки тому
Most simple tutorial ever seen bro, Thank you very much. U helped me lot
@anchitmittal6546
@anchitmittal6546 4 роки тому
The best video for the beginners to understand the MVVM. Thank you
@safees3165
@safees3165 5 років тому
I also learned that if you need the application context in your viewmodel, just extend Androidviewmodel instead of ViewModel
@rickardelimaa
@rickardelimaa 4 роки тому
Couldn't that lead to memory leaks? The whole point with the ViewModel is to avoid Context all together.
@fipabrate
@fipabrate 4 роки тому
Thank you so much, this was more than helpful!
@zersaeger
@zersaeger 2 роки тому
Great explanation. First minute watched and i know what its about. Havent seen any better explanation than that.
@RAHULPAWAR654
@RAHULPAWAR654 4 роки тому
Thanks for tutorial , you have explained Android MVVM way easier than other tutorials on youtube.
@codingwithmitch
@codingwithmitch 4 роки тому
Watch this next for a much better mvvm example codingwithmitch.com/courses/rest-api-mvvm-retrofit2/
@b.k4142
@b.k4142 5 років тому
Thanks again mitch!!
@JayeshBabuAV
@JayeshBabuAV 4 роки тому
thank u for this tutorial. I have a question. If I want to update 'mIsUpdating' variable from the repository, what is the recommended way to do it?
@venukapavarapu1356
@venukapavarapu1356 5 років тому
dudee...Just excellent this is really what i want
@TechMalaya
@TechMalaya 4 роки тому
Thank you for explaining difference mutablelivedata vs livedata. I am very confused when to used both of them because they are basically almost same.
@barathan8766
@barathan8766 2 роки тому
Awesome. Using your vids for my bachelor thesis.!!! thank you
@orhanavan7222
@orhanavan7222 5 років тому
Yes I need this right now :) Please more
@weihenglu691
@weihenglu691 4 роки тому
The best Viewmodel video you can find, end!
@wenbinliu6600
@wenbinliu6600 3 роки тому
Hey Mitch, I am a fan of your tutorials! One question to you for this tutorial: is it a good way to make the repository as a singleton? If that is the way, the singleton instance will be always alive and the livedata inside won't ever be released. I know this makes sense to the demo scenario because it is pure a places app hence we expect the data to be alive for ever. however I think you can explain a little bit for the cases sometimes we don't want to make the repository as singleton, and it should be bind to the lifecycle owner? Just my 2 cents, I am a rookie to Android, especially to LiveData(Jetpack).
@stalker1361
@stalker1361 4 роки тому
It was great. 🙏 and, so helpful. 🙏 Thanks, Dear Mitch
@begmyratmammedov9357
@begmyratmammedov9357 2 роки тому
Excellent explanation! Thanks
@critikalfade
@critikalfade 4 роки тому
Nicely explained, thanks!
@chesdasann3345
@chesdasann3345 2 роки тому
your video is very helpful and well thought out
@krishanmadushanka9521
@krishanmadushanka9521 5 років тому
Nice explanation!
@sathidpy.1622
@sathidpy.1622 4 роки тому
Thank you.
@sadgurushortsinternational
@sadgurushortsinternational 5 років тому
Nice job man keep going
@rayalevinson9803
@rayalevinson9803 5 років тому
Thank you for the movie. One important comment: new nicePlace is added in MainActivityViewModel (and not in NicePlaceRepository). I would like to see how to run postValue from the NivePlaceRepository. Thank you.
@tabarekmohammed6613
@tabarekmohammed6613 3 роки тому
Thats good info u are give to the world for free ... big love
@andrejuntermanns7660
@andrejuntermanns7660 4 роки тому
5:57 The user is a datasource,too. Activity/Fragment editText.getText.toString is data.The user of the app is ALWAYS a Datasource
@sayanthss5793
@sayanthss5793 3 роки тому
Well explained the concept.
@justbrian8286
@justbrian8286 3 роки тому
Nice job man. I have a question though. I am working with data from an API, and every time the fragment refreshes, the recyclerview is repopulated and thus the items are all @t can I solve that?
@chillandcodeyt8065
@chillandcodeyt8065 2 роки тому
Pretty awesome work.
@minamcvinnie4629
@minamcvinnie4629 3 роки тому
These are interesting tutorials. The Java is a little odd to follow, though. Are you going to make versions of these videos in Kotlin?
@williamsteinkedemello5217
@williamsteinkedemello5217 4 роки тому
Hey friend, greetings from Brazil, do you know if there's any project with this MVVM architecture where i can use as template to build up any app in it? Your video is teaching me a lot about this issue, thank you in advance !
@IsanOn
@IsanOn 4 роки тому
thanks mitch, love it
@codingwithmitch
@codingwithmitch 4 роки тому
You should watch the full course, you'll really love that. codingwithmitch.com/courses/rest-api-mvvm-retrofit2/
@IsanOn
@IsanOn 4 роки тому
@@codingwithmitch of course mitch ;)
@ramdanariadi770
@ramdanariadi770 4 роки тому
finally I understand, Thanks alot
@Nidvoraich
@Nidvoraich 3 роки тому
Thanks a lot, man, bro! ;)
@Quang1498
@Quang1498 4 роки тому
setNicePlaces(); function means a action to get data from webservice, right? it's usually asynchronous, so how data can set in livedata sir? using interface to pass data instead?
@HamimKiVines
@HamimKiVines 5 років тому
Hello i am from India, You are Amazing brother, Thank you...
@HamimKiVines
@HamimKiVines 5 років тому
Thanks
@meisammansourzadeh7779
@meisammansourzadeh7779 5 років тому
The hardest part in implementing MVVM is dealing with different data source like cache and local database and network, so your app can be useful even in situations like poor internet. I hope you use these different data sources in your upcoming tutorial
@codingwithmitch
@codingwithmitch 5 років тому
I'm going to do a rest API integration with MVVM. I'll prob add a cache too. Depends on how many members I get on my website. I can't spend too much time on a single course
@adityabansal2737
@adityabansal2737 3 роки тому
Hey Mitch, i ve a question, why did you used different packages for classes in it. They could have been in one package. How does it help ? Thanks in advance
@mohammadrezabooshehri6621
@mohammadrezabooshehri6621 5 років тому
Thanks so much!
@scarswell12
@scarswell12 3 роки тому
Great video. I'm gonna have to rewatch it a few times though. As someone new to design patterns it was hard to follow.
@adibazhar1115
@adibazhar1115 4 роки тому
What other things that I need to understand before learning MVVM other than Room,retrofit ?
@theonlyarjun
@theonlyarjun 5 років тому
whaaaaaaaaaaat !!!! i made the right decision subscribing you man !!!!
@safees3165
@safees3165 5 років тому
Hey Mitch, with repositories, am I supposed to make a single repository for all network operations on my app or multiple repositories with network operations that are relevant to each other?
@codingwithmitch
@codingwithmitch 5 років тому
Sorry I didn't see this comment until now. I would make different repositories to keep it organized. Example: Suppose you had to query "User" account data and also "News" feed data from a server. I would make two repositories. UserRepository and NewsRepository. Each repository would contain all the methods for managing the data from the server.
@safees3165
@safees3165 5 років тому
@@codingwithmitch thanks for replying! Makes sense. That's what I have been doing so far
@b.k4142
@b.k4142 5 років тому
thanks a lot!!
@bjugdbjk
@bjugdbjk 3 роки тому
Mitch, why do we need to use postValue(), Instead you should have used setValue() itself, Since onpostexecute runs on the Main thread, setValue should be good enough right? please correct me if my understanding as wrong.
@VipulSharma-qp7hw
@VipulSharma-qp7hw Рік тому
Do we have to use live data if we are using viewmodel and mvvm ? Like I have some values of user name, address, age etc.. so how to store and retrieve them using viewmodel and mvvm ?
@progtom7585
@progtom7585 2 роки тому
thanks! nice tutorial
@junaidijaz41
@junaidijaz41 5 років тому
you should also make the default constructor of NicePlaceRepository class private. in order to implement the proper singleton method.
@codingwithmitch
@codingwithmitch 5 років тому
Yes you're right
@nareshns7738
@nareshns7738 10 місяців тому
Please continue this topic to adavance..🙏🖤
@ZeeshanKhan-id4fw
@ZeeshanKhan-id4fw 5 років тому
hello mitch i was trying to fetch the data from firebase database using MVVM and following this lecture but it is not working,at the end mutableLivedata showing null,
@sudhir1274
@sudhir1274 4 роки тому
I have modified the code by moving the function to add data to repository class. The viewmodel has links to live data in repository. The code compiles without error and runs. Can you confirm one query on update.
@MrMannyvanny
@MrMannyvanny 4 роки тому
Other than ViewModel advantage,configuration change, what's the real benefit of using MVVM considering all the hassle and fuss involved in MVVM along with Retrofit? Assume I was to create a simple app that just makes network request using Volley without the need of worrying screen rotation, and DAO, what's the advantage we are getting for banging head learning complex architecture that can be achieved in just 10 liner code? It's no doubt good for complex projects though since it's well structured and Unit testing friendly. But what for a simple app?
@rajeshkadiri5268
@rajeshkadiri5268 3 роки тому
Great work Mitch. But i have one doubt as per understanding ViewModel data should clear when we click back button in the mobile and activity is destoryed Right. Iam trying to reopen the app then recycler view Items going on increase. Is there any leak the program.
@saifbutt3038
@saifbutt3038 2 роки тому
Does data get automatically updated in the app when its updated on the server or we have to call api again to get the latest data from server?
@yogeshpol4414
@yogeshpol4414 4 роки тому
Thank u for the tutorial...
@krishanmadushanka9521
@krishanmadushanka9521 3 роки тому
Where do we write the business logic in mvvm? In some articles it says Model and in some other articles it says Viewmodel.
@medicalpump9275
@medicalpump9275 4 роки тому
Excellent
@leonardosibela
@leonardosibela 5 років тому
I really love your videos and you've been helping me a lot... But on your repository, the method is synchronous and on a real example, it would be asynchronous and I'm not sure how I'd do that. Should I create an interface for every method of my repository that has a method for when my request goes ok (a 200, for example) and another method for when there's an error (e.g a 500)?
@codingwithmitch
@codingwithmitch 5 років тому
It's just an example. Realistically you would return LiveData from the repository.
@hemaladani4510
@hemaladani4510 4 роки тому
Beautiful.
@codingwithmitch
@codingwithmitch 4 роки тому
Mvvm is Beauty
@rahulh6711
@rahulh6711 3 роки тому
Brilliant video
@bismeetsingh352
@bismeetsingh352 4 роки тому
Sometimes the live data tends to emit old values. I use SingleLiveEvent() as a replacement. Any other solutions?
@LI11041989
@LI11041989 4 роки тому
That's the main feature, it emits value that it has when you reobserving it, I'm sure it happens due to your lifecycle, you can use Subject by the way
@anishantony1172
@anishantony1172 5 років тому
Wow Man..Thanks a lot
@TheRahul0110
@TheRahul0110 4 роки тому
Hey Mitch! There's a problem. If I actually call an online API in setNicePlaces() method in Repository class, and set the value of dataset in the onResponse(), the LiveData doesn't update the UI with the received data. How should that be implemented? Please help !!!!
@maxmustermann7954
@maxmustermann7954 4 роки тому
Great Video! One Question: In your Repo in setNicePlaces(): Let's assume you wouldn't statically add all values but fetch them asynchronously (e.g. make a JSONObjectRequest), how would you implement this? In my case my fragment which observes changes for some reason only gets an update when a configuration change occurs.
@codingwithmitch
@codingwithmitch 4 роки тому
Take a look at the repository in the google sample for mvvm Architecture github.com/googlesamples/android-architecture-components/blob/master/GithubBrowserSample/app/src/main/java/com/android/example/github/repository/RepoRepository.kt
@maxmustermann7954
@maxmustermann7954 4 роки тому
@@codingwithmitch Thank you. I ended up having two MutableLiveData instances in my ViewModel. The first one is MutableLiveData and the second one MutableLiveData (representing the strings that should be displayed on a spinner dropdown). I observe changes on the first one in order to change the string one. Do you think this is a bad solution?
@maxmustermann7954
@maxmustermann7954 4 роки тому
@@codingwithmitch btw. you've got a new subscriber ^^
@codingwithmitch
@codingwithmitch 4 роки тому
​@@maxmustermann7954 Sounds good to me
@nanjuks6865
@nanjuks6865 2 роки тому
cool explanation ... great...
@codervlogs4921
@codervlogs4921 3 роки тому
Very good explanation
@neerajagandla4366
@neerajagandla4366 4 роки тому
Hey Mitch, I thoroughly enjoyed the tutorial. I want to build a Scientific Calculator app to apply what I learnt here. Can I simply make the calculations in the ViewModel ? Where do I need a repository here unless I want to add a history feature by saving the previous calculations to SQLite? Can we use MVVM for any app no matter the size of the app? I'm not using a specific architecture at my workplace yet. So I'm working on some simple side apps to get started. I have some confusion on the role of viewmodel and the repository - especially for the apps that don't require much SQLite or Network operations. Please clarify.
@codingwithmitch
@codingwithmitch 4 роки тому
Even if you don't have any network operations or db interactions (which basically all apps do), you can use the ViewModel to essentially "cache" data. So if the user rotates the screen or minimizes for any amount of time, the variables will still be there.
@neerajagandla4366
@neerajagandla4366 4 роки тому
What can I do in the Repository part?
@codingwithmitch
@codingwithmitch 4 роки тому
@@neerajagandla4366 codingwithmitch.com/courses/rest-api-mvvm-retrofit2/
@FckYouPlz
@FckYouPlz 4 роки тому
Line 46 (28:24) - you are getting back the new list from the viewmodel everytime the observer knows about a change but you do nothing with the new list that you receive - so why bother notifying the adapter for any changes as you do nothing with these changes anyway?
Android Data Binding: Getting Started
46:04
CodingWithMitch
Переглядів 97 тис.
What is the MVVM pattern, What benefits does MVVM have?
22:38
James Montemagno
Переглядів 90 тис.
ДРУГА РЕПЕТИЦІЯ alyona alyona та Jerry Heil на сцені Євробачення-2024
00:34
Євробачення Україна | Eurovision Ukraine official
Переглядів 235 тис.
КАК ГЛОТАЮТ ШПАГУ?😳
00:33
Masomka
Переглядів 2,2 млн
Which Software Architecture Should You Use: MVC, MVP, or MVVM?
24:27
ArjanCodes
Переглядів 103 тис.
MVVM в Android на практике
41:32
Тимофей Коваленко (Android teacher)
Переглядів 45 тис.
MVVM and LiveData on Android
10:41
CodingWithMitch
Переглядів 36 тис.
Models - WPF MVVM TUTORIAL #1
15:21
SingletonSean
Переглядів 184 тис.
How to Use MVVM (Model View ViewModel) in Swift & iOS - 2023
16:50
iOS Academy
Переглядів 56 тис.
What's new in Python 3.13?
5:08
Carberra
Переглядів 6 тис.
iOS Development is Easier than Android Development (VLOG)
20:41
CodingWithMitch
Переглядів 51 тис.
Intro to Model-View-ViewModel (MVVM) Pattern for WPF in C#
23:07
David Anderson
Переглядів 349 тис.
ДРУГА РЕПЕТИЦІЯ alyona alyona та Jerry Heil на сцені Євробачення-2024
00:34
Євробачення Україна | Eurovision Ukraine official
Переглядів 235 тис.