‫Room database operations such as insertions, updates and queries should not be executed on the main
‫UI thread because they can potentially block the UI causing the app to become unresponsive.
‫We need to offload these database operations to background threads.
‫By doing this, you keep the UI thread free to handle user interactions and ensure that your app remains
‫responsive.
‫When we move to the Kotlin sections, we will use the coroutines that will do these operations in the
‫background.
‫But in Java we need to use handler and executer service in order to execute the methods in the background.
‫So I can't write like this.
‫Add contact and call the contact Dao insert method directly because this will be executed in the main
‫UI thread making problems for our application and cause A and R application not responding or maybe
‫crashes.
‫So to prevent any possible error we need to use handlers and the executer services in order to do this
‫job or this operation.
‫In the background I'll use executer service, I'll name it as executor and alt+ enter to import this
‫class equals to executer dot new single thread executer and executer service.
‫Usually a thread pool executor is used to offload these database operations to background threads.
‫By doing this, you keep the UI thread free to handle user interactions and ensure that your app remains
‫responsive as single threaded executer is created, meaning that database operation will be executed
‫sequentially in a background thread.
‫This can help avoid concurrency issues when dealing with database access.
‫Then the second step is to use the handler Alt+ enter to import the handler and make sure that you use
‫the handler from the OS package equal to new handler.
‫And here I need to use the looper dot, get main looper.
‫Why we use handlers while database operations occur in the background, you often need to update the
‫UI with the results of these operations.
‫However, UI updates must be performed on the main UI thread to avoid view related issues.
‫The handler with Looper dot get main Looper method is used to post tasks to the main UI threads messages
‫queue.
‫This ensures that any UI related code such as updating text views or recycler views is executed on the
‫main thread.
‫So we need to execute the add context in the background.
‫So I use the executor service.
‫I need to update the UI, the text views or the recycler view.
‫So for that I use the handler with Looper by combining an executor service with the handler.
‫This is for background database operations and handler for UI updates.
‫You achieve many benefits such as thread separation synchronization and responsive UI thread separation
‫background Database operations are separated from UI updates, preventing UI blocking and an application
‫not responding errors.
‫Synchronization.
‫Concurrent access to the database is managed by executer service, ensuring that database operations
‫occur sequentially and avoiding race condition.
‫Responsive UI updates are performed on the main thread using the handler, ensuring a responsive user
‫interface.
‫I need to use the executor to execute this method in the background and I'll use the handler to update
‫the UI.
‫So for that I need to use the executor dot execute method.
‫And here I need to use a new runnable and you can see I override it.
‫The run method.
‫I need to move this method here into the run.
‫Method Runnable is an interface that defines a task to be executed.
‫Typically on a separate thread.
‫It represents a unit of work that can be executed as synchronously.
‫Runnable objects are commonly used with thread related APIs, including executer service to perform
‫background tasks, and the runnable interface defines a single method void run that is executed when
‫the runnable task is.
‫Executed.
‫Runnable objects are used to encapsulate code that you want to run asynchronously on a separate thread
‫By submitting a runnable to an executor service, you delegate the execution of that task to the thread
‫managed by the executor.
‫So in order to execute this code in the background, we need to create an executor which is used for
‫background database operations, executor service.
‫Then we need to use the handler for used for the updating the UI and linking this executor with runnable
‫is used to provide the run method that will execute this code asynchronously on separate thread.
‫So this is how in Java we perform background tasks.
‫I'll do with the same with the other methods.
‫Delete and get contacts and I'll move this code and here I need to initialize it in the repository constructor
‫and I'll make it as public in order to use it in the other methods and prevent the duplication of codes.
‫So here executor and I need to cut this handler and handler.
‫Okay, this is from the repository constructor and I'll use the executor in the other methods such as
‫delete contact.
‫So here, executor, I need to provide new runnable and inside the run I need to move this method and
‫execute it in the background.