Search code examples
androidkotlinandroid-roomandroid-architecture-componentsandroid-livedata

How can I delete a LiveData<Object> using Room and LiveData inside an Activity?


I'm making an Android App using Kotlin and the Android Architecture Components (LiveData and Room).

I have an activity where I display an User (that is stored in a DB and recovered using a query with a ViewModel).

I must have the options to edit or delete the current user. The problem is that when I try delete the user the app crashes, but when I open it again the users is gone.

This is my full class code:

class DetallesPerfilActivity : AppCompatActivity() {

private var user_id : Int = -1
lateinit var usuarioViewModel: UsuarioViewModel
private lateinit var usuarioActual : Usuario

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_detalles_perfil)

    toolbar.title = getString(R.string.detalle_usuario)
    setSupportActionBar(toolbar)
    val ab = supportActionBar
    ab!!.setDisplayHomeAsUpEnabled(true)

    user_id = intent.getIntExtra("USER_ID", -1)

    usuarioViewModel = ViewModelProviders.of(this).get(UsuarioViewModel::class.java)

    usuarioViewModel.getUsuario(user_id).observe(this, Observer {
        usuarioActual = it!!
        populateUserFieldsFromDB()
    })

}

override fun onCreateOptionsMenu(menu: Menu): Boolean {
    menuInflater.inflate(R.menu.menu_edit,menu)
    return true
}

override fun onOptionsItemSelected(item: MenuItem?): Boolean {
    when(item?.itemId){
        android.R.id.home ->{
            onBackPressed()
            return true
        }
        R.id.edit_item ->{
            val builder = AlertDialog.Builder(this@DetallesPerfilActivity)
            builder.setItems(R.array.dialogo_editar_eliminar){
                dialog, which ->
                when(which){
                    0-> {
                        val nav = Intent(this@DetallesPerfilActivity, RegistrarUsuarioActivity::class.java)
                        nav.putExtra("USER_ID", user_id)
                        startActivityForResult(nav,349)
                    }
                    1 -> {
                        val innerBuilder = AlertDialog.Builder(this@DetallesPerfilActivity)
                        innerBuilder.setTitle(getString(R.string.eliminar_usuario))
                                .setMessage(getString(R.string.esta_seguro_que_desea_eliminar_usuario))
                                .setPositiveButton(getString(R.string.si)){
                                    dialog, id ->
                                    deleteUser()
                                }
                                .setNegativeButton(getString(R.string.no)){
                                    dialog, id ->
                                }
                        val innerDialog = innerBuilder.create()
                        innerDialog.show()
                    }
                }
            }
            val dialog = builder.create()
            dialog.show()
            return true
        }
    }
    return super.onOptionsItemSelected(item)
}


private fun populateUserFieldsFromDB(){

        NombreApellidosUsuarioTV.text = "${usuarioActual.nombre} ${usuarioActual.apellidos}"
        GeneroUsuarioTV.text = usuarioActual.genero
        EdadUsuarioTV.text = usuarioActual.edad.toString()

}

private fun deleteUser(){
        usuarioViewModel.delete(usuarioActual)
        Toast.makeText(this@DetallesPerfilActivity,getString(R.string.usuario_eliminado_correctamente),Toast.LENGTH_SHORT).show()
        finish()
}

}

And the error that I got is this in the logcat:

    2019-01-10 22:29:09.573 2984-3066/? E/WindowManager: win=Window{6686c65 u0 com.kps.spart.moskimedicationreminder/com.kps.spart.moskimedicationreminder.MainActivity EXITING} destroySurfaces: appStopped=false win.mWindowRemovalAllowed=false win.mRemoveOnExit=false win.mViewVisibility=8, caller=com.android.server.wm.AppWindowToken.destroySurfaces:748 com.android.server.wm.AppWindowToken.destroySurfaces:732 com.android.server.wm.WindowState.onExitAnimationDone:5523 com.android.server.wm.AppWindowAnimator.stepAnimationLocked:517 com.android.server.wm.AppWindowToken.stepAppWindowsAnimation:1745 
2019-01-10 22:29:12.443 1269-1269/com.kps.spart.moskimedicationreminder E/ViewRootImpl: sendUserActionEvent() returned.
2019-01-10 22:29:12.465 2984-3066/? E/WindowManager: win=Window{667c3c2 u0 com.kps.spart.moskimedicationreminder/com.kps.spart.moskimedicationreminder.DetallesPerfilActivity EXITING} destroySurfaces: appStopped=false win.mWindowRemovalAllowed=true win.mRemoveOnExit=true win.mViewVisibility=0, caller=com.android.server.wm.AppWindowToken.destroySurfaces:748 com.android.server.wm.AppWindowToken.destroySurfaces:732 com.android.server.wm.WindowState.onExitAnimationDone:5523 com.android.server.wm.WindowStateAnimator.stepAnimationLocked:553 com.android.server.wm.DisplayContent.lambda$-com_android_server_wm_DisplayContent_21292:465 
2019-01-10 22:29:13.591 1269-1269/com.kps.spart.moskimedicationreminder E/ViewRootImpl: sendUserActionEvent() returned.
2019-01-10 22:29:13.634 1269-1269/com.kps.spart.moskimedicationreminder E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.kps.spart.moskimedicationreminder, PID: 1269
    kotlin.KotlinNullPointerException
        at com.kps.spart.moskimedicationreminder.DetallesPerfilActivity$onCreate$1.onChanged(DetallesPerfilActivity.kt:43)
        at com.kps.spart.moskimedicationreminder.DetallesPerfilActivity$onCreate$1.onChanged(DetallesPerfilActivity.kt:23)
        at android.arch.lifecycle.LiveData.considerNotify(LiveData.java:109)
        at android.arch.lifecycle.LiveData.dispatchingValue(LiveData.java:126)
        at android.arch.lifecycle.LiveData.setValue(LiveData.java:282)
        at android.arch.lifecycle.LiveData$1.run(LiveData.java:87)
        at android.os.Handler.handleCallback(Handler.java:790)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:7000)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:441)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1408)
2019-01-10 22:29:13.732 1958-1958/? E/Zygote: isWhitelistProcess - Process is Whitelisted
2019-01-10 22:29:14.519 4660-4799/? E/PBSessionCacheImpl: sessionId[20496443867522706] not persisted.
2019-01-10 22:29:15.547 2984-3636/? E/Watchdog: !@Sync 1101 [2019-01-10 22:29:15.547]
2019-01-10 22:29:34.739 2027-2027/? E/FeatureClassSet: [#CMH#] Rubin package not supported 
2019-01-10 22:29:43.943 6319-6637/? E/BtGatt.GattService: [GSIM LOG]: gsimLogHandler, msg: MESSAGE_SCAN_START, appName: com.google.uid.shared, scannerId: 4, reportDelayMillis=0
2019-01-10 22:29:44.619 2052-2052/? E/Zygote: isWhitelistProcess - Process is Whitelisted
2019-01-10 22:29:45.540 3655-3695/? E/RequestManager_FLP: [LocationManagerService] Location remove 552b5a4 from system
2019-01-10 22:29:45.554 2984-3636/? E/Watchdog: !@Sync 1102 [2019-01-10 22:29:45.554]
2019-01-10 22:29:45.896 6319-6637/? E/BtGatt.GattService: [GSIM LOG]: gsimLogHandler, msg: MESSAGE_SCAN_STOP, appName: com.google.uid.shared, scannerId: 4, reportDelayMillis=0
2019-01-10 22:29:54.628 2081-2081/? E/Zygote: isWhitelistProcess - Process is Whitelisted
2019-01-10 22:29:54.963 2081-2081/? E/zygote: The String#value field is not present on Android versions >= 6.0
2019-01-10 22:29:55.147 2081-2081/? E/TTS: Unparsable line in file with voice data checksums: voices-list.dev/signature.sf 7846532c8eb3d4d374813dae6d74638b
2019-01-10 22:29:55.147 2081-2081/? E/TTS: Unparsable line in file with voice data checksums: voices-list.dev/voices-list-dsig.pb c1024b1416240bb24b316bac696f5cdb
2019-01-10 22:29:55.148 2081-2081/? E/TTS: Unparsable line in file with voice data checksums: voices-list.rel/signature.sf 93ee1641133be6e6d8cb83934833cd8c
2019-01-10 22:29:55.148 2081-2081/? E/TTS: Unparsable line in file with voice data checksums: voices-list.rel/voices-list-rsig.pb 8ad16260ab46941c146c4598d78862ee
2019-01-10 22:29:55.574 2081-2107/? E/native: compressed_store.h:386 Read: Failed to read compressed states.
2019-01-10 22:30:13.464 3531-3531/? E/KeyguardFingerPrint: updateFingerprintListeningState#mFingerprintRunningState=0 shouldListenForFingerprint=true
2019-01-10 22:30:13.464 3531-3531/? E/KeyguardFingerPrint: startListeningForFingerprint()

So, How can I delete an LiveData Object using a ViewModel without crashing the app?


Solution

  • So, I finally found the solution to this problem, The error was produced when I delete the usuario with usuarioViewModel the observer get a null reference, to solve this problem I need to remove the LiveData's observer and deleting the user after that.

    First I keep a reference to the current LiveData:

        usuarioViewModel = ViewModelProviders.of(this).get(UsuarioViewModel::class.java)
    
        usuarioActualLive =  usuarioViewModel.getUsuario(user_id)
        usuarioActualLive.observe(this, Observer {
            populateUserFieldsFromDB(it)
    
        })
    

    So, When I want to delete the current user I remove the anonymous observer with:

    private fun deleteUser(){
        if(usuarioActualLive.hasObservers()){
            usuarioActualLive.removeObservers(this@DetallesPerfilActivity)
            usuarioViewModel.delete(usuarioActualLive.value!!)
            finish()
        }
    }