0

As this document tells:

enter image description here

So I've returned a object TaskStat from the database(all tables in database are created successfully). My Dao is:

Dao.class

    @Query("SELECT * FROM task_statistics WHERE owner_ID = :ownerId")
    TaskStat getTaskStatOf(long ownerId);

Then I've called the getTaskStaOf() method from Repository class and further from ViewModel class. Here is the code for Repository & ViewModel:

Repository

public TaskStat getTaskStatOf(Task task) {
        return taskDao.getTaskStatOf(task.getTaskId());
    }

ViewModel.class

if (task.getProgress() == task.getMaxProgress()) {
                TaskStat taskStat = taskRepository.getTaskStatOf(task);
                taskStat.setStatus(true);
                taskRepository.updateStat(taskStat);
            }

But when I reach the first line of if() condition in ViewModel the app crushes.. Log says:

at androidx.room.RoomDatabase.assertNotMainThread(RoomDatabase.java:267)
        at androidx.room.RoomDatabase.query(RoomDatabase.java:323)
        at androidx.room.util.DBUtil.query(DBUtil.java:83)
        at taskHelper.TaskDao_Impl.getTaskStatOf(TaskDao_Impl.java:276)
        at taskHelper.TaskRepository.getTaskStatOf(TaskRepository.java:86)
        at taskHelper.TaskViewModel.incrementProgress(TaskViewModel.java:46)
        at com.example.mohasaba.MainActivity$2.onItemLongClick(MainActivity.java:71)
        at taskHelper.TaskAdapter$TaskHolder$2.onLongClick(TaskAdapter.java:113)
        at android.view.View.performLongClickInternal(View.java:7205)
        at android.view.View.performLongClick(View.java:7163)
        at android.view.View.performLongClick(View.java:7181)
        at android.view.View$CheckForLongPress.run(View.java:27294)
        at android.os.Handler.handleCallback(Handler.java:883)
        at android.os.Handler.dispatchMessage(Handler.java:100)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7356)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)

I'm confused where I'm doing wrong! Can you help me?

philoopher97
  • 772
  • 1
  • 6
  • 18
  • 1
    This error tells that Room doesn't allow to interact with Sqlite (both queries and changes) on Main Thread. You should switch all these operations on another thread. There are many answers about that on SO, [here](https://stackoverflow.com/questions/44167111/android-room-simple-select-query-cannot-access-database-on-the-main-thread) one of them – sergiy tikhonov May 14 '20 at 21:18

1 Answers1

0

You can do this. In the ViewModel class change this lines:

if (task.getProgress() == task.getMaxProgress()) {
                TaskStat taskStat = taskRepository.getTaskStatOf(task);
                taskStat.setStatus(true);
                taskRepository.updateStat(taskStat);
            }

to:

if (task.getProgress() == task.getMaxProgress()) {
                    Thread thread = new Thread(new Runnable() {
                        @Override
                        public void run() {
                            TaskStat taskStat = taskRepository.getTaskStatOf(task);
                            taskStat.setStatus(true);
                            taskRepository.updateStat(taskStat);
                        }
                    });
                    thread.start();
                }

Hope this will work.

philoopher97
  • 772
  • 1
  • 6
  • 18