Giter VIP home page Giter VIP logo

Comments (6)

MJ1998 avatar MJ1998 commented on September 23, 2024 1

Could it be because you are calling FhirEngine.get on UI thread ?
May be try wrapping your call with withContext(Dispatchers.IO) { }

from android-fhir.

MJ1998 avatar MJ1998 commented on September 23, 2024 1

From discussion with @yigit, this particular read api should be executed independently of any transaction.

Reason (in Yigt's words): The transaction you add there doesn't really help with anything because you are only doing a read inside that transaction and it is a single read. SQLite will already do a "read transaction" for it implicitly, which will make that consistent.

Would you like to work on this @LZRS ?

from android-fhir.

LZRS avatar LZRS commented on September 23, 2024

Could it be because you are calling FhirEngine.get on UI thread ? May be try wrapping your call with withContext(Dispatchers.IO) { }
Using the withContext(Dispatchers.IO) { }, the delay still seems to happen

Also added a test with this branch

diff --git a/engine/src/androidTest/java/com/google/android/fhir/db/impl/DatabaseImplTest.kt b/engine/src/androidTest/java/com/google/android/fhir/db/impl/DatabaseImplTest.kt
index 16446abf..31a1e35f 100644
--- a/engine/src/androidTest/java/com/google/android/fhir/db/impl/DatabaseImplTest.kt
+++ b/engine/src/androidTest/java/com/google/android/fhir/db/impl/DatabaseImplTest.kt
@@ -50,12 +50,16 @@ import com.google.android.fhir.testing.readJsonArrayFromFile
 import com.google.android.fhir.versionId
 import com.google.common.truth.Correspondence
 import com.google.common.truth.Truth.assertThat
+import kotlinx.coroutines.Dispatchers
+import kotlinx.coroutines.delay
 import java.math.BigDecimal
 import java.time.Instant
 import java.util.Date
 import kotlinx.coroutines.flow.collect
 import kotlinx.coroutines.flow.flowOf
+import kotlinx.coroutines.launch
 import kotlinx.coroutines.runBlocking
+import kotlinx.coroutines.withContext
 import org.hl7.fhir.r4.model.Address
 import org.hl7.fhir.r4.model.CarePlan
 import org.hl7.fhir.r4.model.CodeableConcept
@@ -120,7 +124,7 @@ class DatabaseImplTest {
   private fun buildFhirService(customSearchParameter: List<SearchParameter>? = null) {
     services =
       FhirServices.builder(context)
-        .inMemory()
+//        .inMemory()
         .apply {
           if (encrypted) enableEncryptionIfSupported()
           setSearchParameters(customSearchParameter)
@@ -150,6 +154,27 @@ class DatabaseImplTest {
     assertResourceEquals(TEST_PATIENT_2, database.select(ResourceType.Patient, TEST_PATIENT_2_ID))
   }
 
+  @Test
+  fun select_transactionDelay() {
+    runBlocking {
+      database.insert(TEST_PATIENT_1)
+
+      launch {
+        println("Transaction started")
+        database.withTransaction {
+          delay(3000)
+        }
+        println("Transaction completed")
+      }
+
+      launch {
+        println("Patient Loading")
+        database.select(ResourceType.Patient, TEST_PATIENT_1_ID)
+        println("Patient Loaded")
+      }
+    }
+  }
+
   @Test
   fun update_existentResource_shouldUpdateResource() = runBlocking {
     val patient = Patient()

It printed out

05-25 01:50:02.327 12533 12556 I TestRunner: started: select_transactionDelay[encrypted=false](com.google.android.fhir.db.impl.DatabaseImplTest)
05-25 01:50:03.777 12533 12556 I System.out: Transaction started
05-25 01:50:03.777 12533 12556 I System.out: Patient Loading
05-25 01:50:06.782 12533 12556 I System.out: Transaction completed
05-25 01:50:06.791 12533 12556 I System.out: Patient Loaded
05-25 01:50:06.796 12533 12556 I TestRunner: finished: select_transactionDelay[encrypted=false](com.google.android.fhir.db.impl.DatabaseImplTest)

from android-fhir.

LZRS avatar LZRS commented on September 23, 2024

Removing the db.withTransaction from the select method

diff --git a/engine/src/main/java/com/google/android/fhir/db/impl/DatabaseImpl.kt b/engine/src/main/java/com/google/android/fhir/db/impl/DatabaseImpl.kt
index b156b467..4fab6b5a 100644
--- a/engine/src/main/java/com/google/android/fhir/db/impl/DatabaseImpl.kt
+++ b/engine/src/main/java/com/google/android/fhir/db/impl/DatabaseImpl.kt
@@ -174,12 +174,17 @@ internal class DatabaseImpl(
   }
 
   override suspend fun select(type: ResourceType, id: String): Resource {
-    return db.withTransaction {
-      resourceDao.getResource(resourceId = id, resourceType = type)?.let {
+//    return db.withTransaction {
+//      resourceDao.getResource(resourceId = id, resourceType = type)?.let {
+//        iParser.parseResource(it)
+//      }
+//        ?: throw ResourceNotFoundException(type.name, id)
+//    } as Resource
+
+    return resourceDao.getResource(resourceId = id, resourceType = type)?.let {
         iParser.parseResource(it)
-      }
+      } as? Resource
         ?: throw ResourceNotFoundException(type.name, id)
-    } as Resource
   }
 
   override suspend fun insertSyncedResources(resources: List<Resource>) {

It prints

05-25 02:05:02.355 12752 12775 I TestRunner: started: select_transactionDelay[encrypted=false](com.google.android.fhir.db.impl.DatabaseImplTest)
05-25 02:05:03.771 12752 12775 I System.out: Transaction started
05-25 02:05:03.771 12752 12775 I System.out: Patient Loading
05-25 02:05:03.776 12752 12775 I System.out: Patient Loaded
05-25 02:05:06.773 12752 12775 I System.out: Transaction completed
05-25 02:05:06.779 12752 12775 I TestRunner: finished: select_transactionDelay[encrypted=false](com.google.android.fhir.db.impl.DatabaseImplTest)

from android-fhir.

MJ1998 avatar MJ1998 commented on September 23, 2024

@yigit Need your help here.
I did some research but couldn't find anything on google. Only found one suggestion to make DAO layer asynchronous by providing a flow (or LiveData) of values. But I don't think this will help the above issue.

from android-fhir.

LZRS avatar LZRS commented on September 23, 2024

From discussion with @yigit, this particular read api should be executed independently of any transaction.

Reason (in Yigt's words): The transaction you add there doesn't really help with anything because you are only doing a read inside that transaction and it is a single read. SQLite will already do a "read transaction" for it implicitly, which will make that consistent.

Would you like to work on this @LZRS ?

Yeah, I would like to work on the issue.

Also on the same, would it also affect reads using FhirEngine#search , that joins to other tables?

from android-fhir.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.