Imran Syed commented on issue #5 at Practical Apps / HAMtrainer

Imran Syed
2021-04-29T20:09:39Z

Hi William,

I am unable to make a MR since I do not have access to project. I am attaching a file below with the fix along with the Description.

Issue 1

Calling getWritableDatabase() has two different flows that are executed Flow 1 - When the DB does not exists (First Time) this method call its callback "onCreate" which is responsible to executing the code and adding any data desired Flow 2 - When the DB exists, this method simply fetches the DB without calling its callback.

Fix 1 and Details

In older operating systems, the android throws exception when getWritableDatabase() method follows the Flow#1. It is not able retrieve a lock on DB instance after something is written in it. So as a fix, we catch the exception that is thrown by the system, and we try to fetch the object again by calling getWritableDatabase()

Since calling getWritableDatabase() again will follow the Flow 2, the System is able to retrieve the already created DB this time without throwing any exception.

Issue 2

In importDatabaseFromSQLFile() the code is beginning a transaction. However, this method is called from "onCreate" which returns the DB instance that is ALREADY IN SESSION. calling begin transaction again throws exception in the older operating system (Cannot have transaction inside another transaction). This seems to be handled very well for us for the new operating system. However, the older ones crash.

Fix 2 and Details

Close the transaction if the DB is already in Transaction. Repository.java