Skip to content

Commit 15bbd27

Browse files
committed
Coroutine + Realm = FUCK
1 parent 468aea6 commit 15bbd27

File tree

16 files changed

+28
-29
lines changed

16 files changed

+28
-29
lines changed

common/src/main/java/org/wbpbp/preshoes/interactor/UseCase.kt

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,8 @@
1919

2020
package org.wbpbp.preshoes.interactor
2121

22-
import kotlinx.coroutines.GlobalScope
23-
import kotlinx.coroutines.MainScope
24-
import kotlinx.coroutines.async
25-
import kotlinx.coroutines.launch
22+
import android.os.Handler
23+
import android.os.Looper
2624
import org.wbpbp.preshoes.functional.Result
2725
import timber.log.Timber
2826

@@ -31,18 +29,21 @@ import timber.log.Timber
3129
* Any use case in this application should implement this.
3230
*/
3331
abstract class UseCase<in Params, out Type> {
34-
abstract suspend fun run(params: Params): Result<Type>
32+
abstract fun run(params: Params): Result<Type>
3533

3634
/**
37-
* Execute [run] in Global Scope co-routine and launch onResult on Main conversation.
35+
* Use thread instead of coroutine because it ruins Realm.
3836
*/
3937
operator fun invoke(params: Params, onResult: (Result<Type>) -> Unit = {}) {
40-
Timber.i("Use case '${this::class.java.name}' running")
41-
42-
val job = GlobalScope.async { run(params) }
43-
44-
MainScope().launch {
45-
onResult(job.await())
46-
}
38+
Thread {
39+
try {
40+
Timber.v("UseCase ${this::class.java.name} running on ${Thread.currentThread().name}")
41+
val result = run(params)
42+
Handler(Looper.getMainLooper()).post { onResult(result) }
43+
} catch (e: Exception) {
44+
Timber.w("Exception inside another thread.")
45+
Timber.w(e)
46+
}
47+
}.start()
4748
}
4849
}

data/src/main/java/org/wbpbp/preshoes/storage/ReportRepositoryImpl.kt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,6 @@ class ReportRepositoryImpl(
9494
rightPressure = features.samplesInSingleWalkCycleRight
9595
)
9696

97-
Timber.i(requestModel.toString())
98-
9997
val response = api.requestReportCommentary(requestModel).execute()
10098

10199
if (!response.isSuccessful) {

domain/src/main/java/org/wbpbp/preshoes/usecase/ConnectDevices.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class ConnectDevices(
2727
private val service: SensorDeviceService
2828
) : UseCase<Pair<String, String>, Boolean>() {
2929

30-
override suspend fun run(params: Pair<String, String>)=
30+
override fun run(params: Pair<String, String>)=
3131
Result.of {
3232
val leftResult = service.connectLeftSensorDevice(params.first)
3333
val rightResult = service.connectRightSensorDevice(params.second)

domain/src/main/java/org/wbpbp/preshoes/usecase/CreateReport.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class CreateReport(
2727
private val service: ReportService
2828
) : UseCase<Unit, Int?>() {
2929

30-
override suspend fun run(params: Unit) = Result.of {
30+
override fun run(params: Unit) = Result.of {
3131
service.finishRecordingAndCreateReport()
3232
}
3333
}

domain/src/main/java/org/wbpbp/preshoes/usecase/DeleteReport.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class DeleteReport(
2727
private val repo: ReportRepository
2828
) : UseCase<Int, Unit>() {
2929

30-
override suspend fun run(params: Int) = Result.of {
30+
override fun run(params: Int) = Result.of {
3131
repo.deleteReportById(params)
3232
}
3333
}

domain/src/main/java/org/wbpbp/preshoes/usecase/FinishStandingRecording.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class FinishStandingRecording(
2727
private val service: ReportService
2828
) : UseCase<Unit, Unit>() {
2929

30-
override suspend fun run(params: Unit) = Result.of {
30+
override fun run(params: Unit) = Result.of {
3131
service.finishRecordingStandingPressureDistribution()
3232
}
3333
}

domain/src/main/java/org/wbpbp/preshoes/usecase/FinishWalkingRecording.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class FinishWalkingRecording(
2727
private val service: ReportService
2828
) : UseCase<Unit, Unit>() {
2929

30-
override suspend fun run(params: Unit) = Result.of {
30+
override fun run(params: Unit) = Result.of {
3131
service.finishRecordingWalkingPressureDistribution()
3232
}
3333
}

domain/src/main/java/org/wbpbp/preshoes/usecase/GetReports.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class GetReports(
2929
private val repo: ReportRepository
3030
) : UseCase<Unit, RealmResults<Report>>() {
3131

32-
override suspend fun run(params: Unit) = Result.of {
32+
override fun run(params: Unit) = Result.of {
3333
repo.getAllReports()
3434
}
3535
}

domain/src/main/java/org/wbpbp/preshoes/usecase/GetUser.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class GetUser(
2828
private val repo: UserRepository
2929
) : UseCase<Unit, User?>() {
3030

31-
override suspend fun run(params: Unit) = Result.of {
31+
override fun run(params: Unit) = Result.of {
3232
repo.getUser()
3333
}
3434
}

domain/src/main/java/org/wbpbp/preshoes/usecase/HaltRecording.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class HaltRecording(
2727
private val service: ReportService
2828
) : UseCase<Unit, Unit>() {
2929

30-
override suspend fun run(params: Unit) = Result.of {
30+
override fun run(params: Unit) = Result.of {
3131
service.haltRecording()
3232
}
3333
}

0 commit comments

Comments
 (0)