Why does my database ( Sugar ) return null when I search by id?

Asked

Viewed 50 times

0

I saved in my database using sugar every time I get a new notification, it works correctly, then in a Ragment I populate a recycleview with all database notifications, it also works correctly, but now when I try to delete from the database by id I can’t, my database returns null for some ids ( see the debug image), I’ve tried to understand but I can’t figure out why.

Addition code in the bank:

public void onMessageReceived(RemoteMessage remoteMessage) {
        SugarContext.init( this );
        Log.d("igr", "Mensagem Recebida");
        if (remoteMessage.getData().size() > 0) {
            Log.d("igr", "Message data payload: " + remoteMessage.getData());
            String titulo = remoteMessage.getData().get("title");
            String mensagem = remoteMessage.getData().get("text");

            try{
                Notificacao notificacao= new Notificacao(titulo,mensagem);
                SugarRecord.save(notificacao);
                SugarContext.terminate();
            }catch (Exception e){
                Log.d("igr", "Erro ao salvar notificacao no DB: " + e);
                SugarContext.terminate();
            }

My model:

package br.com.igoroliv.youtubecanal.DataBase;

import com.orm.dsl.Table;

/**
 * Created by igord on 03/07/2017.
 */

@Table
public class Notificacao {

    private Long id;
    private String titulo;
    private String texto;


    public Notificacao(){

    }

    public Notificacao(String titulo, String texto) {
        this.titulo = titulo;
        this.texto = texto;
    }

    /* gets e sets */

    public Long getId() {
        return id;
    }

    public String getTitulo() {
        return titulo;
    }

    public String getTexto() {
        return texto;
    }
}

inserir a descrição da imagem aqui

1 answer

2


I’ve never heard of this Sugar, but from the documentation of it, I believe you should look for the object to delete in this way (because you’re using the notation @Table in the creation of the Model) :

Notificacao notificacao = Notificacao.findById(Notificacao.class,positionToDelete);

Take a look: https://github.com/chennaione/sugar/blob/master/README.md

  • Right the way I’m using, I’ve used without using @table and was giving the same error, the problem is that it depends a lot on the ID, some it deletes, others not .. That’s what I don’t understand

  • Honestly, it tries to implement the base using the native classes: Sqliteopenhelper and Sqlitedatabase. In my opinion, there is much more control doing so.

  • I’ll try this, I wanted to use the sugar because the bank is extremely simple

  • The problem of these "simple" solutions is the complexity to solve the problems, rs

  • refiz in Sqlite but still giving error :/ https://answall.com/questions/218926/porque-meu-delete-do-sqlite-diz-que-deletou-mas-n%C3%A3o-deleted

Browser other questions tagged

You are not signed in. Login or sign up in order to post.