realityexpander / Tasky

Task manager similar to a lite version of Google Calendar
141 stars 12 forks source link

0036 add workmanager sync #39

Closed realityexpander closed 1 year ago

realityexpander commented 1 year ago

QUESTION 1

When attempting to inject the agendaRepository into SyncWorker, I keep getting a runtime error when hilt is instantiating SyncWorker.kt:

10:28:58.769 WM-WorkerFactory         E  Could not instantiate com.realityexpander.tasky.agenda_feature.data.common.workers.SyncWorker
                                         java.lang.NoSuchMethodException: com.realityexpander.tasky.agenda_feature.data.common.workers.SyncWorker.<init> [class android.content.Context, class androidx.work.WorkerParameters]
                                            at java.lang.Class.getConstructor0(Class.java:2332)
                                            at java.lang.Class.getDeclaredConstructor(Class.java:2170)
                                            at androidx.work.WorkerFactory.createWorkerWithDefaultFallback(WorkerFactory.java:95)
                                            at androidx.work.impl.WorkerWrapper.runWorker(WorkerWrapper.java:245)
                                            at androidx.work.impl.WorkerWrapper.run(WorkerWrapper.java:137)
                                            at androidx.work.impl.utils.SerialExecutor$Task.run(SerialExecutor.java:91)
                                            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
                                            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
                                            at java.lang.Thread.run(Thread.java:923)
10:28:58.769 WM-WorkerWrapper         E  Could not create Worker com.realityexpander.tasky.agenda_feature.data.common.workers.SyncWorker

When agendaRepository is not injected, there is no runtime error.

https://github.com/realityexpander/Tasky/blob/a5716b05b8e170bd8b4552cf75f454cfe28e7c26/app/src/main/java/com/realityexpander/tasky/agenda_feature/data/common/workers/SyncWorker.kt#L11-L16

FOLLOW UP TO QUESTION 1

Turns out I was attempting to inject the WorkManager into my ViewModel, and thats not the correct way as the WorkManager is integrated into the Context, which is all system/ui layer. I refactored to use the Application class to start the sync operations and it all works great now.