3

I'm working on a project trying for the first time Kotlin, RxJava 2, and the new android architecture components. I'm trying to execute a delete method from Room on separate Thread and I'm getting ERROR: Fatal signal 11 (SIGSEGV), code 1, fault addr 0x0 in tid 12673 (RxCachedThreadS) when executing the method, and the force closes. I'm trying these two options:

1.

Single.fromCallable { deviceViewModel.delete(device.phone) }
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe()

2.

Observable.just(Unit)
            .subscribeOn(Schedulers.io())
            .map { deviceViewModel.delete(device.phone) }
            .subscribe()

Here's my DeviceViewModel.kt (using AndroidViewModel)

class DeviceViewModel constructor(application: Application) : AndroidViewModel(application) {

@Inject lateinit var db: AppDatabase

init {
    (application as KaiApplication).appComponent.inject(this)
}

fun count() = db.deviceModel().count();

fun getDevices() = db.deviceModel().devices();

fun getDevice(phone: String) = db.deviceModel().device(phone)

fun create(device: Device): Device {
    db.deviceModel().createOrUpdate(device).let {
        return device
    }
}

fun createTestDevice(name: String, phone: String) {
    db.deviceModel().createTestDevice(name, phone)
}

fun delete(device: Device): Device {
    db.deviceModel().delete(device).let {
        return device
    }
}

fun delete(devicePhone: String) {
    db.deviceModel().delete(devicePhone)
}

And my DeviceDao.kt

@Dao
abstract class DeviceDao {

    @Query("SELECT COUNT(*) FROM device")
    abstract fun count(): Int

    @Query("SELECT * FROM device")
    abstract fun devices(): Flowable<List<Device>>

    @Query("SELECT * FROM device WHERE phone = :p0")
    abstract fun device(phone: String): Device?

    fun createOrUpdate(device: Device) {
        Timber.d("Device to create $device")
        insertOrUpdate(device)
    }

    fun createTestDevice(name: String, phone: String) {
        val device: Device = Device()
        device.phone = phone
        device.name = name
        device.password = "12345"

        Timber.d("Device to create $device")
        insertOrUpdate(device)
    }

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    abstract fun insertOrUpdate(vararg devices: Device)

    @Query("DELETE FROM device WHERE phone = :p0")
    abstract fun delete(phone: String)

    @Delete
    abstract fun delete(device: Device)

I've tried to solve this, but no success. Any help would come in hand. Thanks!

live-love
  • 48,840
  • 22
  • 240
  • 204
Josue Mavarez
  • 85
  • 2
  • 8

1 Answers1

1

I had prettyy much the same problem. My scenario was this: I had a RecyclerView list, and each item in the list had a delete button. The delete button fired off an Observable which deleted the item from the Room table. At the same time, the table of items was being observed by another Observable which was updating the RecyclerView which was using a PagedList adapter.

The OP hinted that this might be the problem in comments, just wanted to confirm: after converting my code to use LiveData instead of RxJava, I got a proper error message, which was a FOREIGN KEY CONSTRAINT failure when deleting the item (in my case, I had another entity which was linked to the entity being deleted, and I had onDelete set to RESTRICT).

For some reason, this particular error causes RxJava to absolutely freak out, instead of returning an error message. Seems like a bug.

James Allen
  • 6,406
  • 8
  • 50
  • 83