I have a Room database with two entities. One of them requires a prepopulation, so I have added a callback to the builder in order to this.
For some reason the callback isn't invoked . I've read the answer here, and I do call a DAO method (which returns a LiveData object, if it matters, but I have also tried a calling a DAO method which returns a non-LiveData type), but the issue persists.
The weird thing is that after I clean the app's data from the device's settings, and open the app again, the callback is invoked. This happens both on my physical device and on an AVD one.
Help will be much appreciated.
This is my Database Class:
@Database(entities = {MusicItem.class,
FormatItem.class}, version = 11)
@TypeConverters(Converters.class)
public abstract class ItemsRoomDatabase extends RoomDatabase {
private static final String TAG = ItemsRoomDatabase.class.getSimpleName();
private static ItemsRoomDatabase INSTANCE;
static ItemsRoomDatabase getDatabase(final Context context) {
if (INSTANCE == null) {
synchronized (ItemsRoomDatabase.class) {
if (INSTANCE == null) {
INSTANCE = Room.databaseBuilder(context.getApplicationContext(),
ItemsRoomDatabase.class, "items_database")
.fallbackToDestructiveMigration()
.addCallback(new Callback() {
@Override
public void onCreate(@NonNull SupportSQLiteDatabase db) {
Log.d(TAG, "onCreate callback");
super.onCreate(db);
db.execSQL("INSERT INTO format_items_table (id, name) VALUES (1, 'Unlabeled')");
db.execSQL("INSERT INTO format_items_table (id, name) VALUES (2, 'CD')");
db.execSQL("INSERT INTO format_items_table (id, name) VALUES (3, 'Vinyl')");
}
})
.build();
}
}
}
return INSTANCE;
}
public abstract MusicItemDao musicItemDao();
public abstract FormatItemDao formatItemDao();
}
These are my entities classes (parts that seem relevant to me): MusicItem:
@Entity(tableName = "music_items_table",
indices ={@Index("formatId")},
foreignKeys = @ForeignKey(entity = FormatItem.class,
parentColumns = "id", childColumns = "formatId", onDelete = NO_ACTION))
class MusicItem implements Parcelable {
enum Classification {
COLLECTION,
WISHLIST,
NONE;
}
@PrimaryKey
@NonNull
private final String id;
@NonNull
private String title;
@NonNull
private String artist;
private final String thumbUrl;
private List<String> genres;
@NonNull
private List<Track> tracks;
private String year;
@NonNull
private Classification classification;
@NonNull
private int formatId;
class Converters {
private static Gson gson;
private static Gson getGson() {
if (gson == null) {
gson = new Gson();
}
return gson;
}
// Type converters must be public
@SuppressWarnings("WeakerAccess")
@TypeConverter
public static String fromGenreList(List<String> genres) {
return getGson().toJson(genres);
}
// Type converters must be public
@SuppressWarnings("WeakerAccess")
@TypeConverter
public static List<String> toGenreList(String genres) {
return getGson().fromJson(genres, new TypeToken<List<String>>() {
}.getType());
}
// Type converters must be public
@SuppressWarnings("WeakerAccess")
@TypeConverter
public static String fromTrackList(List<MusicItem.Track> tracks) {
return getGson().toJson(tracks);
}
// Type converters must be public
@SuppressWarnings("WeakerAccess")
@TypeConverter
public static List<MusicItem.Track> toTrackList(String tracks) {
return getGson().fromJson(tracks, new TypeToken<List<MusicItem.Track>>() {
}.getType());
}
// Type converters must be public
@SuppressWarnings("WeakerAccess")
@TypeConverter
public static String fromClassification(MusicItem.Classification classification) {
return classification.name();
}
// Type converters must be public
@SuppressWarnings("WeakerAccess")
@TypeConverter
public static MusicItem.Classification toClassification(String classification) {
return MusicItem.Classification.valueOf(classification);
}
}
FormatItem:
@Entity(tableName = "format_items_table")
class FormatItem {
@PrimaryKey(autoGenerate = true)
private int id;
@NonNull
private final String name;
FormatItem(String name) {
this.name = name;
}
int getId() {
return id;
}
String getName() {
return name;
}
void setId(int id) {
this.id = id;
}
@NonNull
@Override
public String toString() {
return getName();
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
FormatItem that = (FormatItem) o;
return id == that.id &&
name.equals(that.name);
}
}
These are my DAOs:
@Dao
abstract class FormatItemDao {
@Insert
abstract void insert(FormatItem formatItem);
@Query("SELECT name FROM format_items_table WHERE id = :position ORDER BY name ASC")
abstract String getFormatName(int position);
@Query("SELECT COUNT(*) FROM format_items_table")
abstract LiveData<Integer> getNumOfFormatsLiveData();
@Query("SELECT * from format_items_table ORDER BY id ASC")
abstract List<FormatItem> getFormats();
@Query("SELECT * from format_items_table ORDER BY id ASC")
abstract LiveData<List<FormatItem>> getFormatsLiveData();
@Delete
abstract void delete(FormatItem formatItem);
@Query("SELECT name from format_items_table WHERE name = :name COLLATE NOCASE")
abstract String queryName(String name);
@Query("SELECT * from format_items_table WHERE id = :formatId")
abstract FormatItem getFormatById(int formatId);
}
@Dao
abstract class MusicItemDao {
@Insert
abstract void insert(MusicItem musicItem);
@Query("DELETE FROM music_items_table WHERE classification = 'COLLECTION'")
abstract void deleteAllFromCollection();
@Query("DELETE FROM music_items_table WHERE classification = 'WISHLIST'")
abstract void deleteAllFromWishlist();
@Query("SELECT * FROM music_items_table WHERE classification = 'COLLECTION' ORDER BY title ASC")
abstract LiveData<List<MusicItem>> getAllCollectionItems();
@Query("SELECT * from music_items_table WHERE classification = 'WISHLIST' ORDER BY title ASC")
abstract LiveData<List<MusicItem>> getAllWishlistItems();
@Query("SELECT * from music_items_table WHERE id = :id")
abstract MusicItem getItem(String id);
@Query("SELECT id from music_items_table WHERE id = :id")
abstract String queryId(String id);
@Query("DELETE FROM music_items_table WHERE id = :id AND classification = :classification")
abstract void remove(String id, String classification);
@Update
abstract void update(MusicItem musicItem);
@Query("SELECT COUNT(*) FROM music_items_table WHERE formatId = :formatId")
abstract Integer getNumOfItemsInFormat(Integer formatId);
@Query("SELECT * FROM music_items_table WHERE formatId = :formatId")
abstract List<MusicItem> getAllItemsInFormat(Integer formatId);
}
The logcat doesn't mention anything room related.