0

I am using Room on Android and programming in Java and am finding myself jumping through hoops to maintain a count field in a table due to some of the callbacks and background thread requirements and I was hoping somebody might have a simple solution.

I am doing an insert into a table for logging purposes. For each ScenarioRun I need to maintain a count. So it is not the count of records in the whole table, just for a given ScenarioRun.

I believe the best place to do this is in the ViewModel when I am inserting the RunLog entry. Ideally I would calculate the count from the Log table using the RunId, so my Dao would have this query in it:

@Query("SELECT COUNT(*) from ScenaroRunLog WHERE scenarioRunId=:scenarioRunId")
int getScenarioRunLogCount(int scenarioRunId);

Which I would pass through my repository to my ViewModel so on an insert, I would increment it:

public void insert(ScenarioRunLog scenarioRunLog) {

    // code to set the count in the object something like:
    // int count = scenarioRunLogRepository.getScenarioRunLogCount(scenarioRunLog.getScenarioRunId);
    // scenarioRunLog.setCount(count);


    scenarioRunLogRepository.insert(scenarioRunLogId, scenarioRunLog);
}

I could do it in the repository but the only way I can see how to do this is by using allowMainThreadQueries(), which doesn't seem like a best practice or using Executors. Basically the suggestions here.

Is there a better way to do things in this case? I have also found simple things to be challenging.

I could also simply load the ScenarioRun in the ViewModel and reset the count at that time, maintaining it in the ViewModel from that point on, but I have the additional challenge of loading the ScenarioRun. I guess I could do this with another AsyncTask for the select, like they do here.

LiveData for RecyclerViews and other UI components are great, but simple queries have seemed to be challenging. Even returning RowId's on inserts has required a lot of work. Looking for some guidance here. I appreciate the help in advance.

lcj
  • 1,355
  • 16
  • 37
  • 1
    Off the cuff, I am skeptical that this field should exist. Usually, that sort of thing is derived when needed, based on other values, like timestamps. Regardless, if the idea is that you are doing Operation A and Operation B on a database, and the two need to be done in unison, you use a transaction. In the world of Room, that is a DAO method with the `@Transaction` annotation. That method in turn calls other DAO methods that represent the individual operations (e.g., one to modify the real data, one to insert a log entity). – CommonsWare Aug 12 '20 at 23:30
  • That's fair. I do have a timestamp so can order the entries and I am viewing them in a RecyclerView so I can add the count when displaying. That's a great suggestion. Maybe you're right. The larger issue of how simple it is to do these types of things using Room still exists, but this is a good suggestion for the problem at hand. – lcj Aug 13 '20 at 00:26

0 Answers0