khushpanchal / Ketch

An Android File downloader library based on WorkManager with pause and resume support.
https://medium.com/@khush.panchal123/ketch-android-file-downloader-library-7369f7b93bd1
395 stars 26 forks source link

Observe Live data null exception #16

Closed Wizardeirr closed 1 month ago

Wizardeirr commented 1 month ago

The previous app process crashed. This is the stack trace of the crash: java.lang.NullPointerException: Parameter specified as non-null is null: method com.ketch.internal.utils.MapperUtilKt.toDownloadModel, parameter at com.ketch.internal.utils.MapperUtilKt.toDownloadModel(Unknown Source:4) at com.ketch.internal.download.DownloadManager$observeDownloadById$$inlined$map$1$2.emit(Emitters.kt:224) at kotlinx.coroutines.flow.internal.SafeCollectorKt$emitFun$1.invoke(SafeCollector.kt:15) at kotlinx.coroutines.flow.internal.SafeCollectorKt$emitFun$1.invoke(SafeCollector.kt:15) at kotlinx.coroutines.flow.internal.SafeCollector.emit(SafeCollector.kt:87) at kotlinx.coroutines.flow.internal.SafeCollector.emit(SafeCollector.kt:66) at kotlinx.coroutines.flow.FlowKtChannelsKt.emitAllImpl$FlowKtChannelsKt(Channels.kt:37) at kotlinx.coroutines.flow.FlowKt__ChannelsKt.emitAll(Channels.kt:30) at kotlinx.coroutines.flow.FlowKt.emitAll(Unknown Source:1) at androidx.room.CoroutinesRoom$Companion$createFlow$1$1.invokeSuspend(CoroutinesRoom.kt:137) at androidx.room.CoroutinesRoom$Companion$createFlow$1$1.invoke(Unknown Source:8) at androidx.room.CoroutinesRoom$Companion$createFlow$1$1.invoke(Unknown Source:4) at kotlinx.coroutines.intrinsics.UndispatchedKt.startUndispatchedOrReturn(Undispatched.kt:78) at kotlinx.coroutines.CoroutineScopeKt.coroutineScope(CoroutineScope.kt:264) at androidx.room.CoroutinesRoom$Companion$createFlow$1.invokeSuspend(CoroutinesRoom.kt:111) at androidx.room.CoroutinesRoom$Companion$createFlow$1.invoke(Unknown Source:8) at androidx.room.CoroutinesRoom$Companion$createFlow$1.invoke(Unknown Source:4) at kotlinx.coroutines.flow.SafeFlow.collectSafely(Builders.kt:61) at kotlinx.coroutines.flow.AbstractFlow.collect(Flow.kt:230) at com.ketch.internal.download.DownloadManager$observeDownloadById$$inlined$map$1.collect(SafeCollector.common.kt:113) at com.mobileitm.mdm.common.KetchHelper$observeDownload$2.invokeSuspend(KetchHelper.kt:82) at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33) at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:108) at kotlinx.coroutines.internal.LimitedDispatcher$Worker.run(LimitedDispatcher.kt:115) at kotlinx.coroutines.scheduling.TaskImpl.run(Tasks.kt:103) at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:584) at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:793) at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:697) at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:684) Suppressed: kotlinx.coroutines.internal.DiagnosticCoroutineContextException: [StandaloneCoroutine{Cancelling}@f7aa4aa, Dispatchers.IO].

When observe live data with ID this error is coming,

In additon, Success is coming 4 times why its like that. I am downloading step by step. I am sending 1 ID but downloading 4 times

khushpanchal commented 1 month ago

Thanks for reporting,

Please use version 2.0.1 of library I have fixed both issue in it.

Reason and workaround of the error: For the 1st part, an exception is coming because when we make an observe call and by that time download entry is not there inside the room it is crashing.

For 2nd part somehow room is observing all the ids, when we make observeDownloadById call to some id, all previous onserve calls get observed.

As a workaround (if using version 2.0.0) You can add some initial delay before calling observeDownloadById or inside flow before collect, call .retry(1) which will retry on exception by that time download entry will be there. Make sure to call .catch() so app will not crash

For second point call .distinctUntilChanged() before collecting