WorkManager的ForceStopRunnable中的SQLiteException

时间:2019-04-16 13:55:34

标签: android android-room android-workmanager

我正在使用android.arch.work:work-runtime-ktx:1.0.1-rc1并收到许多我无法复制的SQLiteException。

例外:

android.database.sqlite.SQLiteException: 
  at android.database.sqlite.SQLiteConnection.nativeOpen (Native Method)
  at android.database.sqlite.SQLiteConnection.open (SQLiteConnection.java:209)
  at android.database.sqlite.SQLiteConnection.open (SQLiteConnection.java:193)
  at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked (SQLiteConnectionPool.java:463)
  at android.database.sqlite.SQLiteConnectionPool.open (SQLiteConnectionPool.java:185)
  at android.database.sqlite.SQLiteConnectionPool.open (SQLiteConnectionPool.java:177)
  at android.database.sqlite.SQLiteDatabase.openInner (SQLiteDatabase.java:808)
  at android.database.sqlite.SQLiteDatabase.open (SQLiteDatabase.java:793)
  at android.database.sqlite.SQLiteDatabase.openDatabase (SQLiteDatabase.java:696)
  at android.app.ContextImpl.openOrCreateDatabase (ContextImpl.java:652)
  at android.content.ContextWrapper.openOrCreateDatabase (ContextWrapper.java:289)
  at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked (SQLiteOpenHelper.java:223)
  at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase (SQLiteOpenHelper.java:163)
  at androidx.sqlite.db.framework.FrameworkSQLiteOpenHelper$OpenHelper.getWritableSupportDatabase (FrameworkSQLiteOpenHelper.java:92)
  at androidx.sqlite.db.framework.FrameworkSQLiteOpenHelper.getWritableDatabase (FrameworkSQLiteOpenHelper.java:53)
  at androidx.room.RoomDatabase.endTransaction (RoomDatabase.java:340)
  at androidx.work.impl.utils.ForceStopRunnable.run (ForceStopRunnable.java:107)
  at java.util.concurrent.ThreadPoolExecutor.runWorker (ThreadPoolExecutor.java:1133)
  at java.util.concurrent.ThreadPoolExecutor$Worker.run (ThreadPoolExecutor.java:607)
  at java.lang.Thread.run (Thread.java:761)

像这样安排工人:

inline fun <reified T : Worker> schedule(interval: Long,
                                             units: TimeUnit,
                                             tag: String,
                                             networkConstraint: NetworkType? = null) {
            WorkManager.getInstance().apply {
                if (getWorkInfosByTag(tag).get()
                                .all { it.state == WorkInfo.State.CANCELLED || it.state == WorkInfo.State.FAILED }) {
                    // Cancel job by tag if the job already scheduled
                    cancelAllWorkByTag(tag)

                    // Create job and set tag
                    val job = PeriodicWorkRequestBuilder<T>(interval, units).addTag(tag)

                    // Set constraint if networkConstraint != null
                    networkConstraint?.let {
                        val constraints = Constraints.Builder()
                                .setRequiredNetworkType(networkConstraint)
                                .build()

                        job.setConstraints(constraints)
                    }

                    // Enqueue periodic job if no previous job with same tag is scheduled.
                    enqueueUniquePeriodicWork(
                            tag,
                            ExistingPeriodicWorkPolicy.KEEP,
                            job.build())
                }
            }
    }

如果重要,则在Application#onCreate()中执行一次工作人员的调度,而在用户交互后稍后再调度另一个工作人员。

工人本身看起来像这样:

internal class SiloWorker(context: Context, params: WorkerParameters)
    : Worker(context, params) {
    override fun doWork(): Result {
        if (isAppInBackground)
            return Result.success()

        doIntenseDatabaseAndNetworkIOOperations()

        return Result.success()
    }
}

,并且计划每15分钟运行一次。

另一个奇怪的事情是,我仅在Google Play控制台的“ Android Vitals”标签上收到了这些例外,但在Firebase crashlytics中却找不到这些例外。

我不知道这些SQLiteException是在前台还是在后台发生,并且有许多设备版本受此异常影响的Android版本不同。

此行为的可能原因是什么,我该如何解决?

1 个答案:

答案 0 :(得分:1)

我们在2.1.0-alpha03的这一方面做了很多改进。尝试试用该版本。