android - 使用带有 Room 的 ForeignKey 的墓碑

标签 android kotlin android-sqlite android-room tombstone

我在使用 Android 架构组件中的 Room 时遇到了一些问题。在我尝试在我的表中实现 @ForeignKey 之前,所有结构都正常工作。如果我尝试插入新项目,则会出现墓碑。

任务实体

@SuppressLint("ParcelCreator")
@Parcelize
@Entity(
    foreignKeys = [(ForeignKey(
        entity = Category::class,
        parentColumns = ["id"],
        childColumns = ["category_id"],
        onDelete = ForeignKey.CASCADE
    ))],
    indices = [(Index(value = ["category_id"]))]
)
data class Task(
    @ColumnInfo(name = "completed_flag") var completed: Boolean = false,
    @ColumnInfo(name = "task_description") var description: String?,
    @ColumnInfo(name = "category_id") var categoryId: Long = 0
) : Parcelable {

    @IgnoredOnParcel
    @ColumnInfo(name = "id")
    @PrimaryKey(autoGenerate = true)
    var id: Long = 0
}

类别实体

@Parcelize
@Entity
data class Category(
    @ColumnInfo(name = "name") var name: String?,
    @ColumnInfo(name = "color") var color: String?
) : Parcelable {

    @IgnoredOnParcel
    @ColumnInfo(name = "id")
    @PrimaryKey(autoGenerate = true)
    var id: Long = 0
}

任务契约(Contract)

fun addTask(task: Task): Observable<Unit>? =
        Observable.fromCallable { taskDao.insertTask(task) }
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())

当我删除 @ForeignKey 注释并将外部字段视为简单字段时,它起作用了。

我尝试过的事情:

  • 将 Rx Flowable 替换为 LiveData
  • 替换为 .observeOn(Schedulers.io())
  • 替换为 Completable.fromAction()
  • 删除 Parcelize,因为它处于实验阶段
  • 移除@ForeignKey索引字段
  • 在构造函数外声明外部字段

墓碑回溯

2018-06-02 21:52:29.940 27583-27583/? A/DEBUG:     #00 pc 00000000  <unknown>
2018-06-02 21:52:29.940 27583-27583/? A/DEBUG:     #01 pc 00025cdd  /system/lib/libsqlite.so
2018-06-02 21:52:29.940 27583-27583/? A/DEBUG:     #02 pc 00025011  /system/lib/libsqlite.so
2018-06-02 21:52:29.940 27583-27583/? A/DEBUG:     #03 pc 0000eb95  /system/lib/libsqlite.so
2018-06-02 21:52:29.940 27583-27583/? A/DEBUG:     #04 pc 0002d60d  /system/lib/libsqlite.so
2018-06-02 21:52:29.940 27583-27583/? A/DEBUG:     #05 pc 0003b409  /system/lib/libsqlite.so
2018-06-02 21:52:29.940 27583-27583/? A/DEBUG:     #06 pc 00010931  /system/lib/libsqlite.so (sqlite3_step+536)
2018-06-02 21:52:29.940 27583-27583/? A/DEBUG:     #07 pc 0007f175  /system/lib/libandroid_runtime.so
2018-06-02 21:52:29.940 27583-27583/? A/DEBUG:     #08 pc 018a0a33  /system/framework/arm/boot-framework.oat (offset 0x15f4000) (android.database.sqlite.SQLiteConnection.nativeExecute+102)
2018-06-02 21:52:29.940 27583-27583/? A/DEBUG:     #09 pc 018a34b9  /system/framework/arm/boot-framework.oat (offset 0x15f4000) (android.database.sqlite.SQLiteConnection.execute+260)
2018-06-02 21:52:29.940 27583-27583/? A/DEBUG:     #10 pc 018b3741  /system/framework/arm/boot-framework.oat (offset 0x15f4000) (android.database.sqlite.SQLiteSession.endTransactionUnchecked+364)
2018-06-02 21:52:29.940 27583-27583/? A/DEBUG:     #11 pc 018b3ee5  /system/framework/arm/boot-framework.oat (offset 0x15f4000) (android.database.sqlite.SQLiteSession.endTransaction+144)
2018-06-02 21:52:29.941 27583-27583/? A/DEBUG:     #12 pc 018ac61d  /system/framework/arm/boot-framework.oat (offset 0x15f4000) (android.database.sqlite.SQLiteDatabase.endTransaction+72)
2018-06-02 21:52:29.941 27583-27583/? A/DEBUG:     #13 pc 000a91c1  /system/lib/libart.so (art_quick_invoke_stub_internal+64)
2018-06-02 21:52:29.941 27583-27583/? A/DEBUG:     #14 pc 004031e9  /system/lib/libart.so (art_quick_invoke_stub+232)
2018-06-02 21:52:29.941 27583-27583/? A/DEBUG:     #15 pc 000b0455  /system/lib/libart.so (_ZN3art9ArtMethod6InvokeEPNS_6ThreadEPjjPNS_6JValueEPKc+136)
2018-06-02 21:52:29.941 27583-27583/? A/DEBUG:     #16 pc 001ec389  /system/lib/libart.so (_ZN3art11interpreter34ArtInterpreterToCompiledCodeBridgeEPNS_6ThreadEPNS_9ArtMethodEPKNS_7DexFile8CodeItemEPNS_11ShadowFrameEPNS_6JValueE+200)
2018-06-02 21:52:29.941 27583-27583/? A/DEBUG:     #17 pc 001e6939  /system/lib/libart.so (_ZN3art11interpreter6DoCallILb0ELb0EEEbPNS_9ArtMethodEPNS_6ThreadERNS_11ShadowFrameEPKNS_11InstructionEtPNS_6JValueE+492)
2018-06-02 21:52:29.941 27583-27583/? A/DEBUG:     #18 pc 003fd663  /system/lib/libart.so (MterpInvokeVirtualQuick+322)
2018-06-02 21:52:29.941 27583-27583/? A/DEBUG:     #19 pc 0009fd94  /system/lib/libart.so (ExecuteMterpImpl+29972)
2018-06-02 21:52:29.941 27583-27583/? A/DEBUG:     #20 pc 001ca783  /system/lib/libart.so (_ZN3art11interpreterL7ExecuteEPNS_6ThreadEPKNS_7DexFile8CodeItemERNS_11ShadowFrameENS_6JValueEb+290)
2018-06-02 21:52:29.941 27583-27583/? A/DEBUG:     #21 pc 001cf2eb  /system/lib/libart.so (_ZN3art11interpreter33ArtInterpreterToInterpreterBridgeEPNS_6ThreadEPKNS_7DexFile8CodeItemEPNS_11ShadowFrameEPNS_6JValueE+114)
2018-06-02 21:52:29.941 27583-27583/? A/DEBUG:     #22 pc 001e691f  /system/lib/libart.so (_ZN3art11interpreter6DoCallILb0ELb0EEEbPNS_9ArtMethodEPNS_6ThreadERNS_11ShadowFrameEPKNS_11InstructionEtPNS_6JValueE+466)
2018-06-02 21:52:29.941 27583-27583/? A/DEBUG:     #23 pc 003fb92b  /system/lib/libart.so (MterpInvokeInterface+826)
2018-06-02 21:52:29.941 27583-27583/? A/DEBUG:     #24 pc 0009c214  /system/lib/libart.so (ExecuteMterpImpl+14740)
2018-06-02 21:52:29.941 27583-27583/? A/DEBUG:     #25 pc 001ca783  /system/lib/libart.so (_ZN3art11interpreterL7ExecuteEPNS_6ThreadEPKNS_7DexFile8CodeItemERNS_11ShadowFrameENS_6JValueEb+290)
2018-06-02 21:52:29.941 27583-27583/? A/DEBUG:     #26 pc 001cf2eb  /system/lib/libart.so (_ZN3art11interpreter33ArtInterpreterToInterpreterBridgeEPNS_6ThreadEPKNS_7DexFile8CodeItemEPNS_11ShadowFrameEPNS_6JValueE+114)
2018-06-02 21:52:29.941 27583-27583/? A/DEBUG:     #27 pc 001e691f  /system/lib/libart.so (_ZN3art11interpreter6DoCallILb0ELb0EEEbPNS_9ArtMethodEPNS_6ThreadERNS_11ShadowFrameEPKNS_11InstructionEtPNS_6JValueE+466)
2018-06-02 21:52:29.941 27583-27583/? A/DEBUG:     #28 pc 003fd663  /system/lib/libart.so (MterpInvokeVirtualQuick+322)
2018-06-02 21:52:29.941 27583-27583/? A/DEBUG:     #29 pc 0009fd94  /system/lib/libart.so (ExecuteMterpImpl+29972)
2018-06-02 21:52:29.941 27583-27583/? A/DEBUG:     #30 pc 001ca783  /system/lib/libart.so (_ZN3art11interpreterL7ExecuteEPNS_6ThreadEPKNS_7DexFile8CodeItemERNS_11ShadowFrameENS_6JValueEb+290)
2018-06-02 21:52:29.941 27583-27583/? A/DEBUG:     #31 pc 001cf2eb  /system/lib/libart.so (_ZN3art11interpreter33ArtInterpreterToInterpreterBridgeEPNS_6ThreadEPKNS_7DexFile8CodeItemEPNS_11ShadowFrameEPNS_6JValueE+114)
2018-06-02 21:52:29.941 27583-27583/? A/DEBUG:     #32 pc 001e691f  /system/lib/libart.so (_ZN3art11interpreter6DoCallILb0ELb0EEEbPNS_9ArtMethodEPNS_6ThreadERNS_11ShadowFrameEPKNS_11InstructionEtPNS_6JValueE+466)
2018-06-02 21:52:29.941 27583-27583/? A/DEBUG:     #33 pc 003fb92b  /system/lib/libart.so (MterpInvokeInterface+826)
2018-06-02 21:52:29.941 27583-27583/? A/DEBUG:     #34 pc 0009c214  /system/lib/libart.so (ExecuteMterpImpl+14740)
2018-06-02 21:52:29.941 27583-27583/? A/DEBUG:     #35 pc 001ca783  /system/lib/libart.so (_ZN3art11interpreterL7ExecuteEPNS_6ThreadEPKNS_7DexFile8CodeItemERNS_11ShadowFrameENS_6JValueEb+290)
2018-06-02 21:52:29.941 27583-27583/? A/DEBUG:     #36 pc 001cf2eb  /system/lib/libart.so (_ZN3art11interpreter33ArtInterpreterToInterpreterBridgeEPNS_6ThreadEPKNS_7DexFile8CodeItemEPNS_11ShadowFrameEPNS_6JValueE+114)
2018-06-02 21:52:29.941 27583-27583/? A/DEBUG:     #37 pc 001e691f  /system/lib/libart.so (_ZN3art11interpreter6DoCallILb0ELb0EEEbPNS_9ArtMethodEPNS_6ThreadERNS_11ShadowFrameEPKNS_11InstructionEtPNS_6JValueE+466)
2018-06-02 21:52:29.941 27583-27583/? A/DEBUG:     #38 pc 003fd663  /system/lib/libart.so (MterpInvokeVirtualQuick+322)
2018-06-02 21:52:29.941 27583-27583/? A/DEBUG:     #39 pc 0009fd94  /system/lib/libart.so (ExecuteMterpImpl+29972)
2018-06-02 21:52:29.941 27583-27583/? A/DEBUG:     #40 pc 001ca783  /system/lib/libart.so (_ZN3art11interpreterL7ExecuteEPNS_6ThreadEPKNS_7DexFile8CodeItemERNS_11ShadowFrameENS_6JValueEb+290)
2018-06-02 21:52:29.941 27583-27583/? A/DEBUG:     #41 pc 001cf2eb  /system/lib/libart.so (_ZN3art11interpreter33ArtInterpreterToInterpreterBridgeEPNS_6ThreadEPKNS_7DexFile8CodeItemEPNS_11ShadowFrameEPNS_6JValueE+114)
2018-06-02 21:52:29.941 27583-27583/? A/DEBUG:     #42 pc 001e691f  /system/lib/libart.so (_ZN3art11interpreter6DoCallILb0ELb0EEEbPNS_9ArtMethodEPNS_6ThreadERNS_11ShadowFrameEPKNS_11InstructionEtPNS_6JValueE+466)
2018-06-02 21:52:29.941 27583-27583/? A/DEBUG:     #43 pc 003fb92b  /system/lib/libart.so (MterpInvokeInterface+826)
2018-06-02 21:52:29.941 27583-27583/? A/DEBUG:     #44 pc 0009c214  /system/lib/libart.so (ExecuteMterpImpl+14740)
2018-06-02 21:52:29.941 27583-27583/? A/DEBUG:     #45 pc 001ca783  /system/lib/libart.so (_ZN3art11interpreterL7ExecuteEPNS_6ThreadEPKNS_7DexFile8CodeItemERNS_11ShadowFrameENS_6JValueEb+290)
2018-06-02 21:52:29.942 27583-27583/? A/DEBUG:     #46 pc 001cf2eb  /system/lib/libart.so (_ZN3art11interpreter33ArtInterpreterToInterpreterBridgeEPNS_6ThreadEPKNS_7DexFile8CodeItemEPNS_11ShadowFrameEPNS_6JValueE+114)
2018-06-02 21:52:29.942 27583-27583/? A/DEBUG:     #47 pc 001e691f  /system/lib/libart.so (_ZN3art11interpreter6DoCallILb0ELb0EEEbPNS_9ArtMethodEPNS_6ThreadERNS_11ShadowFrameEPKNS_11InstructionEtPNS_6JValueE+466)
2018-06-02 21:52:29.942 27583-27583/? A/DEBUG:     #48 pc 003fd663  /system/lib/libart.so (MterpInvokeVirtualQuick+322)
2018-06-02 21:52:29.942 27583-27583/? A/DEBUG:     #49 pc 0009fd94  /system/lib/libart.so (ExecuteMterpImpl+29972)
2018-06-02 21:52:29.942 27583-27583/? A/DEBUG:     #50 pc 001ca783  /system/lib/libart.so (_ZN3art11interpreterL7ExecuteEPNS_6ThreadEPKNS_7DexFile8CodeItemERNS_11ShadowFrameENS_6JValueEb+290)
2018-06-02 21:52:29.942 27583-27583/? A/DEBUG:     #51 pc 001cf2eb  /system/lib/libart.so (_ZN3art11interpreter33ArtInterpreterToInterpreterBridgeEPNS_6ThreadEPKNS_7DexFile8CodeItemEPNS_11ShadowFrameEPNS_6JValueE+114)
2018-06-02 21:52:29.942 27583-27583/? A/DEBUG:     #52 pc 001e691f  /system/lib/libart.so (_ZN3art11interpreter6DoCallILb0ELb0EEEbPNS_9ArtMethodEPNS_6ThreadERNS_11ShadowFrameEPKNS_11InstructionEtPNS_6JValueE+466)
2018-06-02 21:52:29.942 27583-27583/? A/DEBUG:     #53 pc 003fb92b  /system/lib/libart.so (MterpInvokeInterface+826)
2018-06-02 21:52:29.942 27583-27583/? A/DEBUG:     #54 pc 0009c214  /system/lib/libart.so (ExecuteMterpImpl+14740)
2018-06-02 21:52:29.942 27583-27583/? A/DEBUG:     #55 pc 001ca783  /system/lib/libart.so (_ZN3art11interpreterL7ExecuteEPNS_6ThreadEPKNS_7DexFile8CodeItemERNS_11ShadowFrameENS_6JValueEb+290)
2018-06-02 21:52:29.942 27583-27583/? A/DEBUG:     #56 pc 001cf2eb  /system/lib/libart.so (_ZN3art11interpreter33ArtInterpreterToInterpreterBridgeEPNS_6ThreadEPKNS_7DexFile8CodeItemEPNS_11ShadowFrameEPNS_6JValueE+114)
2018-06-02 21:52:29.942 27583-27583/? A/DEBUG:     #57 pc 001e691f  /system/lib/libart.so (_ZN3art11interpreter6DoCallILb0ELb0EEEbPNS_9ArtMethodEPNS_6ThreadERNS_11ShadowFrameEPKNS_11InstructionEtPNS_6JValueE+466)
2018-06-02 21:52:29.942 27583-27583/? A/DEBUG:     #58 pc 003fb92b  /system/lib/libart.so (MterpInvokeInterface+826)
2018-06-02 21:52:29.942 27583-27583/? A/DEBUG:     #59 pc 0009c214  /system/lib/libart.so (ExecuteMterpImpl+14740)
2018-06-02 21:52:29.942 27583-27583/? A/DEBUG:     #60 pc 001ca783  /system/lib/libart.so (_ZN3art11interpreterL7ExecuteEPNS_6ThreadEPKNS_7DexFile8CodeItemERNS_11ShadowFrameENS_6JValueEb+290)
2018-06-02 21:52:29.942 27583-27583/? A/DEBUG:     #61 pc 001cf2eb  /system/lib/libart.so (_ZN3art11interpreter33ArtInterpreterToInterpreterBridgeEPNS_6ThreadEPKNS_7DexFile8CodeItemEPNS_11ShadowFrameEPNS_6JValueE+114)
2018-06-02 21:52:29.942 27583-27583/? A/DEBUG:     #62 pc 001e691f  /system/lib/libart.so (_ZN3art11interpreter6DoCallILb0ELb0EEEbPNS_9ArtMethodEPNS_6ThreadERNS_11ShadowFrameEPKNS_11InstructionEtPNS_6JValueE+466)
2018-06-02 21:52:29.942 27583-27583/? A/DEBUG:     #63 pc 003fb92b  /system/lib/libart.so (MterpInvokeInterface+826)

你有什么想法吗?

最佳答案

我发现了问题。问题是将默认值设置为 task.category_id0如果没有 CategoryTask 有关.

一些代码更改后,我能够看到崩溃日志而不是墓碑:

io.reactivex.exceptions.OnErrorNotImplementedException: FOREIGN KEY constraint failed (code 787)
    at io.reactivex.internal.functions.Functions$OnErrorMissingConsumer.accept(Functions.java:704)
    at io.reactivex.internal.functions.Functions$OnErrorMissingConsumer.accept(Functions.java:701)
    at io.reactivex.internal.observers.LambdaObserver.onError(LambdaObserver.java:77)
    at io.reactivex.internal.operators.observable.ObservableDoOnEach$DoOnEachObserver.onError(ObservableDoOnEach.java:119)
    at io.reactivex.internal.operators.observable.ObservableObserveOn$ObserveOnObserver.checkTerminated(ObservableObserveOn.java:276)
    at io.reactivex.internal.operators.observable.ObservableObserveOn$ObserveOnObserver.drainNormal(ObservableObserveOn.java:172)
    at io.reactivex.internal.operators.observable.ObservableObserveOn$ObserveOnObserver.run(ObservableObserveOn.java:252)
    at io.reactivex.android.schedulers.HandlerScheduler$ScheduledRunnable.run(HandlerScheduler.java:109)
    at android.os.Handler.handleCallback(Handler.java:751)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6123)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:757)
Caused by: android.database.sqlite.SQLiteConstraintException: FOREIGN KEY constraint failed (code 787)
    at android.database.sqlite.SQLiteConnection.nativeExecuteForLastInsertedRowId(Native Method)
    at android.database.sqlite.SQLiteConnection.executeForLastInsertedRowId(SQLiteConnection.java:782)
    at android.database.sqlite.SQLiteSession.executeForLastInsertedRowId(SQLiteSession.java:788)
    at android.database.sqlite.SQLiteStatement.executeInsert(SQLiteStatement.java:86)
    at android.arch.persistence.db.framework.FrameworkSQLiteStatement.executeInsert(FrameworkSQLiteStatement.java:50)
    at android.arch.persistence.room.EntityInsertionAdapter.insert(EntityInsertionAdapter.java:64)
    at com.escodro.alkaa.data.local.TaskDao_Impl.insertTask(TaskDao_Impl.java:90)
    at com.escodro.alkaa.ui.task.TaskContract$addTask$1.call(TaskContract.kt:37)
    at com.escodro.alkaa.ui.task.TaskContract$addTask$1.call(TaskContract.kt:15)

为了修复它,我设置了 task.category_id作为nullable :

data class Task(
    @ColumnInfo(name = "completed_flag") var completed: Boolean = false,
    @ColumnInfo(name = "task_description") var description: String?,
    @ColumnInfo(name = "category_id") var categoryId: Long? = null
) : Parcelable

关于android - 使用带有 Room 的 ForeignKey 的墓碑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50662521/

相关文章:

android - 在谷歌地图android中默认显示特定国家

java - 使用线程和异步任务无法防止 ANR 错误

Android:何时/为什么我应该使用 FrameLayout 而不是 Fragment?

kotlin - 这是 Kotlin REPL 的错误吗?

android - 使用 CASE、WHEN TRUE 时,某些 Android 9、10 设备上出现 SQLiteException

android - 调用函数删除一行

android - Realm 对象服务器 + MSSQL/MySQL

kotlin - 当条件满足时,如何在when block 中执行多个代码?

java - 在 Kotlin 中,为什么我的每个 Float 占用 128 位而不是 16 位?

java - 无法从 CursorWindow 读取第 0 行第 5 列。在访问数据之前确保游标已正确初始化