256 Like 3 Dislike
In part 5 of the Architecture Components tutorial, we will create our Viewmodel class. The ViewModel works as a gateway between the UI controller and the repository. It stores and processes data for the activity/fragment and it doesn't get destoyed on configuration changes, so it doesn't lose it's variable state for example when the device is rotated. By extending AndroidViewModel, we get a handle to the application context, which we then use to instantiate our RoomDatabase. In our activity we let the system provide us the correct ViewModel instance by calling ViewModelProviders.of, where we pass the Activity or Fragment this ViewModel's lifecycle should be scoped to. When our Activity/Fragment is then destroyed, the ViewModel will go through it's onCleared method and get removed from the memory. In our activitie's onCreate method, we retrieve the LiveData that is stored in our ViewModel and call observe on it, where we pass a LifecycleOwner and an Observer. In the onChange callback we get an update on our data whenever something in the corresponding database table changes. The LiveData will automatically start and stop sending updates to our activity at the right time in it's lifecycle and clean up any unused references. Watch the whole playlist: https://www.youtube.com/playlist?list=PLrnPJCHvNZuDihTpkRs6SpZhqgBqPU118 Example code, dependencies & instructions: https://codinginflow.com/tutorials/android/room-viewmodel-livedata-recyclerview-mvvm/part-5-viewmodel ____________________ Subscribe to my channel: https://www.youtube.com/c/codinginflow?sub_confirmation=1 Want more Android tutorials? Check my playlist section: https://www.youtube.com/c/codinginflow/playlists Follow me on social media: Facebook: https://www.facebook.com/codinginflow Instagram: https://www.instagram.com/codinginflow Twitter: https://twitter.com/codinginflow Blog: https://codinginflow.com/
My Top 10 Fav Android TV Boxes of 2018 ➡️ Top 10 TV Box Chart: http://bit.ly/2Od1rgr --- ✅ NVIDIA SHIELD TV (2017) 8 OREO ➡️ Amazon US: https://amzn.to/2JPrVBG ➡️ Amazon UK: https://amzn.to/2JymcB8 ➡️ Amazon DE: https://amzn.to/2yjhe2E ➡️ Amazon CA: https://amzn.to/2ylUMpI ➡️ Amazon IN: https://amzn.to/2OM344t --- ✅ H96 MAX (HEXACORE) v7 ➡️ Banggood: https://goo.gl/HWViwB ➡️ Gearbest: http://bit.ly/2yn2MtG --- ✅ X99 (HEXACORE) v7 ➡️ Banggood: http://bit.ly/2qVl4uS ➡️ Gearbest: http://bit.ly/2tiHNSX --- ✅ XIAOMI MI BOX - 8 OREO ➡️ Bangood: https://goo.gl/W1ehRz ➡️ Gearbest: http://bit.ly/2I6fNHs --- ✅ FIRE TV 4K CUBE ➡️ Amazon US: https://amzn.to/2vdip2V ➡️ Ebay: https://ebay.to/2LMQYag --- ✅ FIRE TV 4K (3RD GEN) ➡️ Amazon US: https://amzn.to/2uXyxHS ➡️ Amazon US: https://amzn.to/2v1blZr --- ✅ MAGICSEE C400 + TV TUNER ➡️ Bangood: http://bit.ly/2MwC20j ➡️ Gearbest: http://bit.ly/2MwX8vO --- ✅ MECOOL M8S PRO L ➡️ Banggood: https://goo.gl/DmWAUc ➡️ Gearbest: http://bit.ly/2MIZnZo --- ✅ H96 MAX PLUS 64GB ➡️ Banggood: http://bit.ly/2MIZRNJ ➡️ Aliexpress: http://bit.ly/2ODVPIn --- ✅ AMAZON FIRE TV STICK 2 ➡️ Amazon US: http://amzn.to/2A10XCr ➡️ Amazon US: http://amzn.to/2i0wk95 ➡️ Amazon DE: https://amzn.to/2y2Bx4T ➡️ Amazon In: https://amzn.to/2y2ByWv --- —————— FREE Trials: 🆓 Amazon Prime FREE Trial US: http://amzn.to/2nD6SIs 🆓 Amazon Prime FREE Trial UK: http://amzn.to/2n4GovS ——————— My Social Links: 🔵 View My Channel - http://youtube.com/ChigzTechReviews 🔵 Follow on Twitter - http://twitter.com/ChigzTechReview 🔵 Follow on Facebook: http://facebook.com/ChigzTechReviews 🔵 View my Website: http://ChigzTech.com 🔵 My Official Email: firstname.lastname@example.org ---
Git Tutorial Playlist : https://goo.gl/V5rppE Python tutorial: https://goo.gl/6LTUJX Git is created by Linus Torvald Git is a Distributed Version Control System. Git is famous for project collaboration More than 1 person can work on a project at the same time In this video we will see : What is git why to use it Why do we need a version control system What is Centralised Version Control System What is Distributed Version Control System What is Trunk Based Development Trainer: Navin Reddy Editing Monitors : https://amzn.to/2RfKWgL https://amzn.to/2Q665JW https://amzn.to/2OUP21a. Editing Laptop : ASUS ROG Strix - (new version) https://amzn.to/2RhumwO Camera : https://amzn.to/2OR56AV lens : https://amzn.to/2JihtQo Mics https://amzn.to/2RlIe9F https://amzn.to/2yDkx5F Check out our website: http://www.telusko.com Follow Telusko on Twitter: https://twitter.com/navinreddy20 Follow on Facebook: Telusko : https://www.facebook.com/teluskolearnings Navin Reddy : https://www.facebook.com/navintelusko Follow Navin Reddy on Instagram: https://www.instagram.com/navinreddy20 Subscribe to our other channel: Navin Reddy : https://www.youtube.com/channel/UCxmkk8bMSOF-UBF43z-pdGQ?sub_confirmation=1 Telusko Hindi : https://www.youtube.com/channel/UCitzw4ROeTVGRRLnCPws-cw?sub_confirmation=1 Subscribe to the channel and learn Programming in easy way. Java Tutorial : https://goo.gl/p10QfB REST Tutorial : https://goo.gl/3mosnz Kotlin Tutorial : https://goo.gl/JedEzX Scala Tutorials for Java Developers : https://goo.gl/8H1aE5 C Tutorial Playlist : https://goo.gl/8v92pu Android Tutorial : https://goo.gl/MzlIUJ XML Tutorial : https://goo.gl/Eo79do Design Patterns in Java : https://goo.gl/Kd2MWE Socket Programming in Java : https://goo.gl/jlMEbg Spring MVC Tutorial : https://goo.gl/9ubbG2 OpenShift Tutorial for Beginners : https://goo.gl/s58BQH Spring Framework with Maven : https://goo.gl/MaEluO Sql Tutorial for Beginners : https://goo.gl/x3PrTg String Handling in Java : https://goo.gl/zUdPwa Array in Java : https://goo.gl/uXTaUy Java Servlet : https://goo.gl/R5nHp8 Exception Handling in Java : https://goo.gl/N4NbAW
This video shows how to build a mobile app connected to an API backend (cloud database) in 5 minutes.
In part 4 of the Architecture Components tutorial, we will create our Repository. The Repository ist a simple Java class that abstracts the data layer from the rest of the app and mediates between different data sources, like a web service and a local cache. It hides the different database operations (like SQLite queries) and provides a clean API to the ViewModel.
Since Room doesn't allow database queries on the main thread, we use AsyncTasks to execute them asynchronously.
LiveData is fetched on a worker thread automatically, so we don't have to take care of this.
Also, we will add a RoomDatabase.Callback to our database builder where we populate our database in the onCreate method so we don't start with an empty table. We can also override onOpen if we want to execute code every time our Room database is opened.
Watch the whole playlist:
Example code, dependencies & instructions:
Subscribe to my channel:
Want more Android tutorials? Check my playlist section:
Follow me on social media: