@@ -33,60 +33,51 @@ import com.google.android.fhir.sync.upload.ResourceConsolidatorFactory
33
33
import com.google.android.fhir.sync.upload.SyncUploadProgress
34
34
import com.google.android.fhir.sync.upload.UploadRequestResult
35
35
import com.google.android.fhir.sync.upload.UploadStrategy
36
- import java.time.OffsetDateTime
36
+ import kotlinx.coroutines.Dispatchers
37
37
import kotlinx.coroutines.flow.Flow
38
38
import kotlinx.coroutines.flow.firstOrNull
39
39
import kotlinx.coroutines.flow.flow
40
40
import kotlinx.coroutines.flow.onEach
41
+ import kotlinx.coroutines.withContext
41
42
import org.hl7.fhir.r4.model.Resource
42
43
import org.hl7.fhir.r4.model.ResourceType
43
44
44
45
/* * Implementation of [FhirEngine]. */
45
46
internal class FhirEngineImpl (private val database : Database , private val context : Context ) :
46
47
FhirEngine {
47
- override suspend fun create (vararg resource : Resource ): List <String > {
48
- return database.insert(* resource)
49
- }
48
+ override suspend fun create (vararg resource : Resource ) =
49
+ withContext(Dispatchers .IO ) { database.insert(* resource) }
50
50
51
- override suspend fun get (type : ResourceType , id : String ): Resource {
52
- return database.select(type, id)
53
- }
51
+ override suspend fun get (type : ResourceType , id : String ) =
52
+ withContext(Dispatchers .IO ) { database.select(type, id) }
54
53
55
- override suspend fun update (vararg resource : Resource ) {
56
- database.update(* resource)
57
- }
54
+ override suspend fun update (vararg resource : Resource ) =
55
+ withContext(Dispatchers .IO ) { database.update(* resource) }
58
56
59
- override suspend fun delete (type : ResourceType , id : String ) {
60
- database.delete(type, id)
61
- }
57
+ override suspend fun delete (type : ResourceType , id : String ) =
58
+ withContext(Dispatchers .IO ) { database.delete(type, id) }
62
59
63
- override suspend fun <R : Resource > search (search : Search ): List <SearchResult <R >> {
64
- return search.execute(database)
65
- }
60
+ override suspend fun <R : Resource > search (search : Search ): List <SearchResult <R >> =
61
+ withContext(Dispatchers .IO ) { search.execute(database) }
66
62
67
- override suspend fun count (search : Search ): Long {
68
- return search.count(database)
69
- }
63
+ override suspend fun count (search : Search ) =
64
+ withContext(Dispatchers .IO ) { search.count(database) }
70
65
71
- override suspend fun getLastSyncTimeStamp (): OffsetDateTime ? {
72
- return FhirEngineProvider .getFhirDataStore(context).readLastSyncTimestamp()
73
- }
66
+ override suspend fun getLastSyncTimeStamp () =
67
+ withContext(Dispatchers .IO ) {
68
+ FhirEngineProvider .getFhirDataStore(context).readLastSyncTimestamp()
69
+ }
74
70
75
- override suspend fun clearDatabase () {
76
- database.clearDatabase()
77
- }
71
+ override suspend fun clearDatabase () = withContext(Dispatchers .IO ) { database.clearDatabase() }
78
72
79
- override suspend fun getLocalChanges (type : ResourceType , id : String ): List <LocalChange > {
80
- return database.getLocalChanges(type, id)
81
- }
73
+ override suspend fun getLocalChanges (type : ResourceType , id : String ) =
74
+ withContext(Dispatchers .IO ) { database.getLocalChanges(type, id) }
82
75
83
- override suspend fun purge (type : ResourceType , id : String , forcePurge : Boolean ) {
84
- database.purge(type, setOf (id), forcePurge)
85
- }
76
+ override suspend fun purge (type : ResourceType , id : String , forcePurge : Boolean ) =
77
+ withContext(Dispatchers .IO ) { database.purge(type, setOf (id), forcePurge) }
86
78
87
- override suspend fun purge (type : ResourceType , ids : Set <String >, forcePurge : Boolean ) {
88
- database.purge(type, ids, forcePurge)
89
- }
79
+ override suspend fun purge (type : ResourceType , ids : Set <String >, forcePurge : Boolean ) =
80
+ withContext(Dispatchers .IO ) { database.purge(type, ids, forcePurge) }
90
81
91
82
override suspend fun syncDownload (
92
83
conflictResolver : ConflictResolver ,
0 commit comments