WorkManager 崩溃试图在 Application#onCreate 中排队唯一的周期性工作
WorkManager crashes trying to enqueue unique periodic work in Application#onCreate
我想将 WorkManager 与 Dagger 2 一起使用,以将依赖项注入 worker。我按照 this article 来实施设置。设置的一部分是按需初始化。为了实现它,我在清单文件中添加了以下行:
<provider
android:name="androidx.work.impl.WorkManagerInitializer"
android:authorities="${applicationId}.workmanager-init"
android:exported="false"
tools:node="remove" />
并使我的应用程序 class 像这样实现 Configuration.Provider
接口:
@NonNull
@Override
public Configuration getWorkManagerConfiguration() {
return new Configuration.Builder().setMinimumLoggingLevel(Log.DEBUG).setWorkerFactory(delegatingWorkerFactory).build();
}
Dagger 设置按预期工作。
但是我遇到了以下问题:在 Application#onCreate
中,我将唯一的定期任务排入队列,并且在第一次安装应用程序时,此操作总是失败并出现以下错误:
java.lang.IllegalStateException: The file system on the device is in a bad state. WorkManager cannot access the app's internal data store.
at androidx.work.impl.utils.ForceStopRunnable.run(ForceStopRunnable.java:115)
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:764)
Caused by: android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14 SQLITE_CANTOPEN): Could not open database
at android.database.sqlite.SQLiteConnection.nativeOpen(Native Method)
at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:211)
at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:195)
at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:503)
at android.database.sqlite.SQLiteConnectionPool.tryAcquireNonPrimaryConnectionLocked(SQLiteConnectionPool.java:987)
at android.database.sqlite.SQLiteConnectionPool.waitForConnection(SQLiteConnectionPool.java:693)
at android.database.sqlite.SQLiteConnectionPool.acquireConnection(SQLiteConnectionPool.java:378)
at android.database.sqlite.SQLiteSession.acquireConnection(SQLiteSession.java:894)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:586)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:46)
at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1408)
at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1383)
at androidx.sqlite.db.framework.FrameworkSQLiteDatabase.query(FrameworkSQLiteDatabase.java:161)
at androidx.room.RoomDatabase.query(RoomDatabase.java:328)
at androidx.room.util.DBUtil.query(DBUtil.java:83)
at androidx.work.impl.model.PreferenceDao_Impl.getLongValue(PreferenceDao_Impl.java:70)
at androidx.work.impl.utils.PreferenceUtils.getNeedsReschedule(PreferenceUtils.java:96)
at androidx.work.impl.utils.ForceStopRunnable.shouldRescheduleWorkers(ForceStopRunnable.java:187)
at androidx.work.impl.utils.ForceStopRunnable.run(ForceStopRunnable.java:88)
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:764)
如果我将使我的工作排队的代码从 Application#onCreate
移动到应用程序生命周期的后期(例如某些 activity),那么它不会失败。
我没有发现 Application#onCreate
对排队工作有任何限制,所以我希望它一定能工作。
我使用的是 WorkManager 2.3.4 和 Room 2.2.5。我尝试将 Room 降级到 2.2.2 并将 WorkManager 降级到几个较旧的稳定版本,但没有帮助。问题可在具有不同 Android 版本的不同设备和模拟器上重现。
如何解决此问题并在 Application#onCreate
中继续排队工作?
编辑
这是我排队工作的方式:
workManagerProvider.get().enqueueUniquePeriodicWork(
"Unique name",
ExistingPeriodicWorkPolicy.REPLACE,
request
)
其中 workManagerProvider
是 Provider<WorkManager>
。由于循环依赖,我无法直接访问 workmanager,因此我不得不从 dagger 获取一个提供程序,然后才使用它。与主题无关
问题是由另一段代码引起的,该代码在首次启动时擦除应用程序的内部数据。擦除是在 WorkManager
的内部数据库创建的同时执行的。因此,在初始化期间 WorkManager
试图访问已删除的数据库文件的 SQLite 连接,这导致了崩溃。
我想将 WorkManager 与 Dagger 2 一起使用,以将依赖项注入 worker。我按照 this article 来实施设置。设置的一部分是按需初始化。为了实现它,我在清单文件中添加了以下行:
<provider
android:name="androidx.work.impl.WorkManagerInitializer"
android:authorities="${applicationId}.workmanager-init"
android:exported="false"
tools:node="remove" />
并使我的应用程序 class 像这样实现 Configuration.Provider
接口:
@NonNull
@Override
public Configuration getWorkManagerConfiguration() {
return new Configuration.Builder().setMinimumLoggingLevel(Log.DEBUG).setWorkerFactory(delegatingWorkerFactory).build();
}
Dagger 设置按预期工作。
但是我遇到了以下问题:在 Application#onCreate
中,我将唯一的定期任务排入队列,并且在第一次安装应用程序时,此操作总是失败并出现以下错误:
java.lang.IllegalStateException: The file system on the device is in a bad state. WorkManager cannot access the app's internal data store.
at androidx.work.impl.utils.ForceStopRunnable.run(ForceStopRunnable.java:115)
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:764)
Caused by: android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14 SQLITE_CANTOPEN): Could not open database
at android.database.sqlite.SQLiteConnection.nativeOpen(Native Method)
at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:211)
at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:195)
at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:503)
at android.database.sqlite.SQLiteConnectionPool.tryAcquireNonPrimaryConnectionLocked(SQLiteConnectionPool.java:987)
at android.database.sqlite.SQLiteConnectionPool.waitForConnection(SQLiteConnectionPool.java:693)
at android.database.sqlite.SQLiteConnectionPool.acquireConnection(SQLiteConnectionPool.java:378)
at android.database.sqlite.SQLiteSession.acquireConnection(SQLiteSession.java:894)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:586)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:46)
at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1408)
at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1383)
at androidx.sqlite.db.framework.FrameworkSQLiteDatabase.query(FrameworkSQLiteDatabase.java:161)
at androidx.room.RoomDatabase.query(RoomDatabase.java:328)
at androidx.room.util.DBUtil.query(DBUtil.java:83)
at androidx.work.impl.model.PreferenceDao_Impl.getLongValue(PreferenceDao_Impl.java:70)
at androidx.work.impl.utils.PreferenceUtils.getNeedsReschedule(PreferenceUtils.java:96)
at androidx.work.impl.utils.ForceStopRunnable.shouldRescheduleWorkers(ForceStopRunnable.java:187)
at androidx.work.impl.utils.ForceStopRunnable.run(ForceStopRunnable.java:88)
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:764)
如果我将使我的工作排队的代码从 Application#onCreate
移动到应用程序生命周期的后期(例如某些 activity),那么它不会失败。
我没有发现 Application#onCreate
对排队工作有任何限制,所以我希望它一定能工作。
我使用的是 WorkManager 2.3.4 和 Room 2.2.5。我尝试将 Room 降级到 2.2.2 并将 WorkManager 降级到几个较旧的稳定版本,但没有帮助。问题可在具有不同 Android 版本的不同设备和模拟器上重现。
如何解决此问题并在 Application#onCreate
中继续排队工作?
编辑 这是我排队工作的方式:
workManagerProvider.get().enqueueUniquePeriodicWork(
"Unique name",
ExistingPeriodicWorkPolicy.REPLACE,
request
)
其中 workManagerProvider
是 Provider<WorkManager>
。由于循环依赖,我无法直接访问 workmanager,因此我不得不从 dagger 获取一个提供程序,然后才使用它。与主题无关
问题是由另一段代码引起的,该代码在首次启动时擦除应用程序的内部数据。擦除是在 WorkManager
的内部数据库创建的同时执行的。因此,在初始化期间 WorkManager
试图访问已删除的数据库文件的 SQLite 连接,这导致了崩溃。