ebook img

Android’s Architecture Components Version 0.10 PDF

425 Pages·2018·3.29 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Android’s Architecture Components Version 0.10

Android’s Architecture Components by Mark L. Murphy Android’s Architecture Components by Mark L. Murphy Copyright © 2017-2018 CommonsWare, LLC. All Rights Reserved. Printed in the United States of America. Printing History: July 2018: Version 0.10 The CommonsWare name and logo, “Busy Coder's Guide”, and related trade dress are trademarks of CommonsWare, LLC. All other trademarks referenced in this book are trademarks of their respective frms. The publisher and author(s) assume no responsibility for errors or omissions or for damages resulting from the use of the information contained herein. Table of Contents Headings formatted in bold-italic have changed since the last version. • Preface ◦ How the Book Is Structured ............................................................... vii ◦ Prerequisites ....................................................................................... viii ◦ About the Updates ............................................................................. viii ◦ What’s New in Version 0.10? .................................................... viii ◦ Warescription ....................................................................................... ix ◦ Book Bug Bounty ................................................................................... x ◦ Source Code and Its License ................................................................. x ◦ Creative Commons and the Four-to-Free (42F) Guarantee ....... xi ◦ Acknowledgments ................................................................................ xi • Room Basics ◦ Wrenching Relations Into Objects ....................................................... 1 ◦ Room Requirements ................................................................... 2 ◦ Room Furnishings .................................................................................. 3 ◦ Get a Room ............................................................................................ 9 • Testing Room ◦ Writing Instrumentation Tests ............................................................ 11 ◦ Writing Unit Tests via Mocks .............................................................. 15 • The Dao of Entities ◦ Configuring Entities ............................................................................ 21 ◦ DAOs and Queries ................................................................................ 31 ◦ Dynamic Queries ................................................................................. 37 ◦ Other DAO Operations ....................................................................... 39 ◦ Transactions and Room ....................................................................... 42 ◦ Threads and Room ............................................................................. 44 • Room and Custom Types ◦ Type Converters ................................................................................... 45 ◦ Embedded Types .................................................................................. 53 ◦ Updating the Trip Sample ................................................................... 56 • Room and Relations ◦ The Classic ORM Approach ................................................................ 61 ◦ A History of Threading Mistakes ........................................................ 62 ◦ The Room Approach ........................................................................... 63 ◦ Plans for Trips ..................................................................................... 64 ◦ Self-Referential Relations for Tree Structures ................................... 72 i ◦ Using @Relation .................................................................................. 74 ◦ @Relation and @Query ....................................................................... 76 ◦ Representing No Relation ................................................................... 76 • The Support Database API ◦ “Can’t You See That This is a Facade?” ............................................... 79 ◦ When Will We Use This? .................................................................... 81 ◦ Configuring Room’s Database Access ................................................. 81 ◦ Consuming the Support Database API Directly ............................... 84 • Room and Migrations ◦ What’s a Migration? ............................................................................ 91 ◦ When Do We Migrate? ........................................................................ 92 ◦ But First, a Word About Exporting Schemas ..................................... 92 ◦ Writing Migrations .............................................................................. 95 ◦ Employing Migrations ....................................................................... 100 ◦ How Room Applies Migrations ......................................................... 102 ◦ Testing Migrations .................................................................. 102 ◦ Migrating Under Protest ................................................................... 107 • Securing Your Room ◦ Meet the Players ................................................................................ 109 ◦ Using CWAC-SafeRoom ..................................................................... 110 ◦ More to Come! .................................................................................... 112 • Lifecycles and Owners ◦ A Tale of Terminology ........................................................................ 115 ◦ Adding the Lifecycle Components ........................................... 116 ◦ Getting a Lifecycle .............................................................................. 117 ◦ Observing a Lifecycle ......................................................................... 119 ◦ Legacy Options ........................................................................ 120 ◦ So, What’s the Point of This? ............................................................ 124 • LiveData ◦ Observables Are the New Black ........................................................ 125 ◦ Yet More Terminology ....................................................................... 126 ◦ Implementing LiveData .......................................................... 127 ◦ Other LiveData Examples .................................................................. 133 ◦ Testing LiveData ................................................................................. 135 • ViewModel ◦ ViewModels, As Originally Envisioned ............................................. 137 ◦ ViewModel Versus… .......................................................................... 138 ◦ Dependencies ..................................................................................... 139 ◦ Mommy, Where Do ViewModels Come From? ............................... 139 ◦ ViewModels, Google’s Way ............................................................... 140 ◦ ViewModels as Simple POJOs ........................................................... 143 ii ◦ ViewModels and Scopes ........................................................... 146 • Other Lifecycle Owners ◦ ProcessLifecycleOwner ...................................................................... 161 ◦ LifecycleService .................................................................................. 165 ◦ Wait… Where Are LifecycleProvider and LifecycleReceiver? ... 165 • LiveData and Data Binding ◦ A Data Binding Recap .............................................................. 167 ◦ LiveData Updating Data Binding ............................................ 170 ◦ Handling Changes to LiveData ............................................... 176 ◦ The Saved Instance State Situation .................................................. 178 • WorkManager ◦ Where Should We Use WorkManager? .................................... 183 ◦ Where Should We Not Use WorkManager? .............................. 184 ◦ WorkManager Dependencies .................................................. 184 ◦ Workers: They Do Work ........................................................... 185 ◦ Performing Simple Work ......................................................... 187 ◦ Work Inputs ............................................................................. 187 ◦ Constrained Work .................................................................... 188 ◦ Tagged Work ............................................................................ 189 ◦ Monitoring Work .................................................................... 190 ◦ Canceling Work ........................................................................ 194 ◦ Delayed Work ........................................................................... 195 ◦ Parallel Work ........................................................................... 195 ◦ Chained Work ..........................................................................196 ◦ Periodic Work ......................................................................... 202 ◦ Unique Work ........................................................................... 202 ◦ Testing Work ........................................................................... 203 • M:N Relations in Room ◦ Implementing a Join Entity .............................................................. 209 ◦ Implementing DAO Methods ............................................................ 213 ◦ Where’s That Good Ol’ Object Feel? ................................................. 215 • Polymorphic Room Relations ◦ Polymorphism With Separate Tables ................................................ 217 ◦ Polymorphism With a Single Table .................................................. 222 ◦ Polymorphism With M:N Relations ................................................. 226 • LiveData Transformations ◦ The Bucket Brigade ........................................................................... 227 ◦ Mapping Data to Data ....................................................................... 228 ◦ Mapping Data to… LiveData? ............................................................ 230 ◦ Writing a Transformation ...................................................... 230 ◦ Do We Really Want This? ................................................................. 232 iii • RxJava and Room ◦ Adding RxJava .......................................................................... 235 ◦ A Quick Sidebar on Rx Testing ......................................................... 236 ◦ Rx Query Response Types ................................................................. 237 ◦ Applying This to Your UI .................................................................. 242 ◦ @RawQuery and Reactive Responses ............................................... 243 • RxJava and Lifecycles ◦ The Classic Approach ........................................................................ 245 ◦ Bridging RxJava and LiveData ............................................... 246 ◦ The Uber Solution: AutoDispose ...................................................... 249 ◦ The Semi-Deprecated Solution: RxLifecycle ................................... 249 • Packing Up a Room ◦ The Problem ........................................................................................ 251 ◦ The Classic Solution: SQLiteAssetHelper ........................................ 252 ◦ The New Problem .............................................................................. 253 ◦ Merging SQLiteAssetHelper with Room .......................................... 253 • Paging Room Data ◦ The Problem: Too Much Data ........................................................... 257 ◦ Addressing the UX ............................................................................. 258 ◦ Enter the Paging Library ................................................................... 258 ◦ Paging and Room .................................................................... 260 ◦ What About RxJava? ......................................................................... 269 • Immutability ◦ The Benefits of Immutability ............................................................ 273 ◦ The Costs of Immutability ................................................................ 275 ◦ Immutability via AutoValue ................................................... 278 • The Repository Pattern ◦ What the Repository Does ................................................................ 283 ◦ High-Level Repository Strategies .................................................... 286 ◦ Let’s Roll the Dice ............................................................................. 288 ◦ Blending Data Sources ...................................................................... 301 • Introducing Model-View-Intent ◦ GUI Architectures ............................................................................... 311 ◦ Why People Worry About GUI Architectures .................................. 312 ◦ Why Others Ignore GUI Architectures ............................................ 314 ◦ A Rough Comparison of GUI Architectures .................................... 316 ◦ The Basics of Model-View-Intent ..................................................... 319 ◦ Additional MVI Resources ................................................................ 323 • A Deep Dive Into MVI ◦ What the Sample App Does .............................................................. 325 ◦ MVI and the Sample App .................................................................. 333 iv ◦ The Model .......................................................................................... 334 ◦ The View State ................................................................................... 336 ◦ The View ............................................................................................ 338 ◦ The Actions ........................................................................................ 342 ◦ Publishing Actions .................................................................. 346 ◦ The Repositories ................................................................................ 348 ◦ The Controller .................................................................................... 353 ◦ About Those Results .......................................................................... 357 ◦ The Reducer in the RosterViewModel ............................................. 359 ◦ Examining the Other Fragments ...................................................... 364 ◦ Summary ............................................................................................ 365 • Backing Up a Room ◦ What Do We Need to Back Up? ............................................... 369 ◦ Beware of Open Rooms ............................................................ 371 ◦ When Do We Back Up the Database? ...................................... 371 ◦ A Basic Backup Example .......................................................... 373 ◦ Backing Up Off-Device ............................................................ 380 • Room and Full-Text Searching ◦ What Is FTS? ...................................................................................... 381 ◦ What You Can’t Do ............................................................................ 382 ◦ The FTS Recipe .................................................................................. 382 ◦ Searching a Book ............................................................................... 384 • Configuring SQLite Beyond Room ◦ When To Make Changes ................................................................... 405 ◦ Example: Turbo Boost Mode ............................................................ 406 v Preface Thanks! Thanks for your interest in Android app development, the world’s most popular operating system! And, thanks for your interest in the Android Architecture Components, released by Google in 2017 to help address common “big-ticket” problems in Android app development. And, most of all, thanks for your interest in this book! I sincerely hope you find it useful! (OTOH, if you find it completely useless… um, don’t tell anyone, OK?) How the Book Is Structured We start off with a look at Room, an object/relational mapping (ORM) library. This makes it a bit easier to integrate your app with SQLite, the built-in relational database engine in Android. We then move into the lifecycle components. These components help you deal with objects that have lifecycles, particularly activities and services. The LiveData class in particular gives you a lightweight “reactive” way of consuming data while still honoring things like configuration changes and the typical activity/fragment destroy-and-recreate cycle. We will also peek at ViewModel, the Architecture Components’ way of helping you maintain state across configuration changes. We will see how other libraries, like data binding and RxJava, can be tied into the Architecture Components. We then move into a discussion of architecture in general, with material on the vii

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.