Testing Queries

With your repository in place, there is one last step before you can test your query functions. Currently, your database is empty, because you have not added any crimes to it. To speed things up, you will upload existing database files to your emulator to populate your database. The database files have been provided for you in the solutions file for this chapter (www.bignerdranch.com/​solutions/​AndroidProgramming4e.zip).

You could programmatically generate and insert dummy data into the database, like the 100 dummy crimes you have been using. However, you have not yet implemented a DAO function to insert new database entries (you will do so in Chapter 14). Uploading a preexisting database file allows you to easily seed the database without altering your app’s code unnecessarily. Plus, it gives you practice using the Device File Explorer to view the contents of your emulator’s local storage system.

Having said that, there is one drawback to this method of testing. You can only access your app’s private files (where the database files are located) when you are on an emulator (or a “rooted” device). You cannot upload the database files to a typical physical device.

Uploading test data

Each application on an Android device has a directory in the device’s sandbox. Keeping files in the sandbox protects them from being accessed by other applications or even the prying eyes of users (unless the device has been rooted, in which case the user can get to whatever they like).

An application’s sandbox directory is a child of the device’s data/data directory named after the application package. For CriminalIntent, the full path to the sandbox directory is data/data/com.bignerdranch.android.criminalintent.

To upload the files, make sure the emulator is running and open the Device File Explorer toolbar window in Android Studio by clicking its tab in the bottom right of the window. The file explorer pane will show all of the files currently on the emulator (Figure 11.2).

Figure 11.2  Device File Explorer window

Device File Explorer window

To find the folder for CriminalIntent, open the data/data/ folder and find the subfolder with the name matching the package ID of the project (Figure 11.3). Files in this folder are private to your app by default, so no other apps can read them. This is where you will upload the database files.

Figure 11.3  App sandbox folder

App sandbox folder

To upload the databases folder, right-click on the package-name folder and select Upload... (Figure 11.4).

Figure 11.4  Uploading database files

Uploading database files

This will show you a file navigator where you can select the files to upload. Navigate to the solutions folder for this chapter. Make sure you upload the entire databases folder. Room needs the files to be in a folder called databases, or it will not work. When you finish the upload, your app sandbox folder should look like Figure 11.5.

Figure 11.5  Uploaded database files

Uploaded database files

With your database files uploaded, you now have data you can query using your repository. Currently, your CrimeListViewModel creates 100 fake crimes to display in the list. Remove the code that generates the fake crimes and replace it with a call to the getCrimes() function on your CrimeRepository.

Listing 11.14  Accessing the repository in ViewModel (CrimeListViewModel.kt)

class CrimeListViewModel : ViewModel() {

    val crimes = mutableListOf<Crime>()

    init {
        for (i in 0 until 100) {
            val crime = Crime()
            crime.title = "Crime #$i"
            crime.isSolved = i % 2 == 0
            crimes += crime
        }
    }

    private val crimeRepository = CrimeRepository.get()
    val crimes = crimeRepository.getCrimes()
}

Now, run your app to see the outcome. You may be surprised to find … your app crashes.

Do not worry, this is the expected behavior. (At least, it is what we expected. Sorry.) Take a look at the exception in Logcat to see what happened.

    java.lang.IllegalStateException: Cannot access database on the main thread since
    it may potentially lock the UI for a long period of time.

This error originates from the Room library. Room is unhappy with your attempt to access the database on the main thread. Throughout the remainder of this chapter, you will learn about the threading model Android uses, as well as the purpose of the main thread and considerations for the type of work you run on the main thread. Additionally, you will move your database interactions to a background thread. That will make Room happy and get rid of the exception, which will in turn fix the crash.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.211.66