android - 使用外键和房间的墓碑
问题描述
我在使用 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
}
任务契约
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_id
of0
以防万一Category
与Task
.
经过一些代码更改后,我能够看到崩溃日志而不是墓碑:
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
推荐阅读
- java - StringTokeniser 不读取数据
- php - 使用 ajax 如何在 ckeditor 中显示查询中的数据
- java - Graal 本机图像中缺少属性文件
- javascript - 如何将标题菜单项重新路由到 URL 而不是 React/Gatsby 中的内部文件?
- arrays - 我怎样才能更好地重构这段代码?
- c# - 检索用 RSA 签名的数据?
- java - Android 默认按钮颜色
- tensorflow - ValueError:两个形状中的维度 0 必须相等,但分别为 10 和 1。形状为 [10] 和 [1]
- javascript - PHP FOREACH LOOP FOR SELECT OPTION 在 JQUERY INSIDE PHP
- graphql - 使用 MDX 在 Gatsby 博客中添加 featuresImage