Many tasks in Android should be done in background threads. It's a requirement for some operations, such as network requests, but it's also strongly recommended when working with persistent storage. Learn about how to manage database operations with the Java Executor class, and how to communicate between background threads and the main thread with the EventBus library.
- [Instructor] Last week I described…how you can use the Room Persistence Library…to create code that manages your SQLite databases.…In that exercise, I ran my code on the main thread…but that's not a recommended approach.…For best performance and to ensure…that you don't freeze the UI thread,…you should always execute your queries…on a background thread.…I'll show you one approach for that.…First, I'll go to my database class,…NotesDatabase in this version of the app…and I'll comment out allowMainThreadQueries.…
Then I'll run the application.…When I click Run Code,…the application crashes.…I'll go to Logcat and show the reason.…I get this message,…cannot access database on the main thread.…Now, this is a rule that's implemented by Room…and as I showed there's an easy way around it…but if you want to run your code…in the background thread,…you have a number of options.…In the course on concurrent programming in Android…I show a variety of strategies…but I'm going to show one more,…using the Java Executor class.…
The executor is designed to manage a thread pool,…
Skill Level Intermediate
Q: Why can't I earn a Certificate of Completion for this course?
A: We publish a new tutorial or tutorials for this course on a regular basis. We are unable to offer a Certificate of Completion because it is an ever-evolving course that is not designed to be completed. Check back often for new movies.
Building Android Apps with Cloud Serviceswith Albert Lo2h 37m Intermediate
Android App Development: Data Persistence Librarieswith Annyce Davis4h 52m Intermediate
New This Week:
1. Android Development Tips Weekly
- Mark as unwatched
- Mark all as unwatched
Are you sure you want to mark all the videos in this course as unwatched?
This will not affect your course history, your reports, or your certificates of completion for this course.Cancel
Take notes with your new membership!
Type in the entry box, then click Enter to save your note.
1:30Press on any video thumbnail to jump immediately to the timecode shown.
Notes are saved with you account but can also be exported as plain text, MS Word, PDF, Google Doc, or Evernote.