Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Room Database Query returning empty list

I am working on a project that uses Room database to store and retrieve objects. I have everything set up properly with RoomDB, Dao and Entity annotations.

When I try to retrieve objects I have previously saved I get an empty list as a result.

This is my method for retrieving items:

class SpotRepository(context: Context) {

    private val itemDAO: SpotDAO = RoomDB.getDB(context).spotDao



    val allSpotItemsList: List<Spot>
        get() = itemDAO.getAllSpotItemsList()
 }

This is my itemDao

@Dao abstract class SpotDAO{

    @Query("SELECT * FROM spotItems")
    abstract fun getAllSpotItems(): LiveData<List<Spot>>

    @Query("SELECT * FROM spotItems")
    abstract fun getAllSpotItemsList(): List<Spot>


    @Query("SELECT * FROM spotItems WHERE uuid = :id")
    abstract fun getSpotById(id: String): Spot

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    abstract fun insertItem(item: Spot): Long

    @Query("DELETE FROM spotItems WHERE id = :id")
    abstract fun deleteItem(id: Long)


    @Update
    abstract fun update(item: Spot): Int }

This is my RoomDB

@Database(entities = {Spot.class}, version = 1)
@TypeConverters({Converters.class})
public abstract class RoomDB extends RoomDatabase {

    private static RoomDB INSTANCE;
    private final static Object lock = new Object();
    public abstract SpotDAO getSpotDao();
    public static RoomDB getDB(Context context){
        synchronized (lock){
            if(INSTANCE == null){
                INSTANCE = Room.databaseBuilder(context, RoomDB.class, "Items.db")
                        .build();
            }
            return INSTANCE;
        }
    }




}

And this is my Entity

@Entity(tableName = "spotItems") @JsonIgnoreProperties("photo", "id") class Spot : Serializable{


    @PrimaryKey(autoGenerate = true)
    var id: Long = 0
    @ColumnInfo(name = "hidden")
    var hidden = false
    @ColumnInfo(name = "address")
    var address = ""
    @ColumnInfo(name = "uuid")
    var uuid = ""
    @ColumnInfo(name = "latitude")
    var latitude: Double = 0.0
    @ColumnInfo(name = "longitude")
    var longitude: Double = 0.0
    @ColumnInfo(name = "message")
    var message = ""
    @ColumnInfo(name = "picture_url")
    var picture_url = ""
    @ColumnInfo(name = "thumb_url")
    var thumb_url = ""
    @ColumnInfo(name = "comment_count")
    var comment_count = 0
    @ColumnInfo(name = "poster_uuid")
    var poster_uuid = "poster_uuid_place_holder"
    @Ignore
    var likes = HashMap<String, Boolean>()
    @Ignore
    var dislikes = HashMap<String, Boolean>()
    @ColumnInfo(name = "timestamp")
    var timestamp = 0L

    //  This needs to only get used for reuploading!!!
    @ColumnInfo(name = "photo")
    var photo: Bitmap? = null

    override fun equals(other: Any?): Boolean {
        if (this === other) return true
        if (javaClass != other?.javaClass) return false

        other as Spot

        if (uuid != other.uuid) return false

        return true
    }

    override fun hashCode(): Int {
        return uuid.hashCode()
    }


}

My problem arises when I try to access the list of items I have saved by using

  val spotsForReupload = SpotRepository(this@MainActivity).allSpotItemsList

                    Log.e("NUMBER", spotsForReupload.size.toString())

Here, the number of items retrieved is always 0

The only helpful hint I get from this is the following error log in my console

E/SQLiteCursor: onMove() return false. RequiredPos: 0 WindowStartPos: 0 WindowRowCount: 0(original count of query: 1)

I do not know where this error is coming from and I do not know what it means

Why am I getting an empty list? Thank you

like image 602
Mihai Fumarel Avatar asked Sep 17 '25 10:09

Mihai Fumarel


1 Answers

Edit: I have done something incredibly dumb. I was trying to save a 10mb bitmap in the form of a byte array in the database. Don't do that

like image 136
Mihai Fumarel Avatar answered Sep 19 '25 08:09

Mihai Fumarel