使用Android Architecture Components的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_id相关的0的情况下,将默认值设置为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/

    10-11 00:28