Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Room doesn't store data

My aim is to store some data into a SQLite database using Room.

So I made a lot of @Entities in POJO.

For each @Entity I made a @Dao with at least these queries:

@Dao
public interface RouteDao {
    @Query("SELECT * FROM route")
    LiveData<List<Route>> getAll();

    @Insert
    void insertAll(List<Route> routes);

    @Query("DELETE FROM route")
    void deleteAll();
}

My Singleton Room @Database is:

@Database(entities = {Agency.class, Calendar.class, CalendarDate.class, FeedInfo.class, Route.class, Stop.class, StopTime.class, Transfer.class, Trip.class}, version = 1)
@TypeConverters(MyConverters.class)
public abstract class GtfsDatabase extends RoomDatabase {

    private static final String DATABASE_NAME = "gtfs-db";

    private static GtfsDatabase INSTANCE;

    public abstract AgencyDao agencyDao();

    public abstract CalendarDao calendarDao();

    public abstract CalendarDateDao calendarDateDao();

    public abstract FeedInfoDao feedInfoDao();

    public abstract RouteDao routeDao();

    public abstract StopDao stopDao();

    public abstract StopTimeDao stopTimeDao();

    public abstract TransferDao transferDao();

    public abstract TripDao tripDao();

    public static synchronized GtfsDatabase getDatabase(Context context) {
        return INSTANCE == null ? INSTANCE = Room.databaseBuilder(
                context.getApplicationContext(),
                GtfsDatabase.class,
                DATABASE_NAME
        ).build() : INSTANCE;
    }
}

When I open the app for the first time, I fill the database with data in a background IntentService:

public static void importData(Context context, Map<String, String> data) {
        GtfsDatabase db = GtfsDatabase.getDatabase(context);
        db.beginTransaction();
        try {
            db.agencyDao().deleteAll();
            db.calendarDao().deleteAll();
            db.calendarDateDao().deleteAll();
            db.feedInfoDao().deleteAll();
            db.routeDao().deleteAll();
            db.stopDao().deleteAll();
            db.stopTimeDao().deleteAll();
            db.transferDao().deleteAll();
            db.tripDao().deleteAll();

            db.agencyDao().insertAll(rawToAgencies(data.get(AGENCY_FILE)));
            db.calendarDao().insertAll(rawToCalendars(data.get(CALENDAR_FILE)));
            db.calendarDateDao().insertAll(rawToCalendarDates(data.get(CALENDAR_DATES_FILE)));
            db.feedInfoDao().insertAll(rawToFeedInfos(data.get(FEED_INFO_FILE)));
            db.routeDao().insertAll(rawToRoutes(data.get(ROUTES_FILE)));
            db.tripDao().insertAll(rawToTrips(data.get(TRIPS_FILE)));
            db.stopDao().insertAll(rawToStops(data.get(STOPS_FILE)));
            db.stopTimeDao().insertAll(rawToStopsTimes(data.get(STOP_TIMES_FILE)));
            db.transferDao().insertAll(rawToTransfers(data.get(TRANSFERS_FILE)));

            PreferenceManager.getDefaultSharedPreferences(context).edit().putBoolean(context.getString(R.string.empty), false).apply();
        } finally {
            db.endTransaction();
        }
    }

I am absolutely sure that the data is correct. I debugged each line and I can say 100% sure that the list of objects I pass to these functions is correct. No error at all.

When this service is finished (if(!sharedPreferences.getBoolean(getString(R.string.empty), true))) I try to access to the database in another activity and this show me that is empty.

I checked with this library debugCompile 'com.amitshekhar.android:debug-db:1.0.0' and every table is really empty. enter image description here

What I'm doing wrong?

I know you cannot see all my code, and maybe there's something wrong, so my actual question is: is the above code correct?

like image 769
gekoramy bean Avatar asked Oct 30 '22 04:10

gekoramy bean


1 Answers

Solved.

Android Room is handling transactions automatically. @Query are asynchronous, while @Insert and @Delete are synchronous.

My error was to try including all those operations in a single transaction. The solution is: let that Room handles them automatically.

public static void importData(Context context, Map<String, String> data) {
        GtfsDatabase db = GtfsDatabase.getDatabase(context);
        db.agencyDao().deleteAll();
        db.calendarDao().deleteAll();
        db.calendarDateDao().deleteAll();
        db.feedInfoDao().deleteAll();
        db.routeDao().deleteAll();
        db.stopDao().deleteAll();
        db.stopTimeDao().deleteAll();
        db.transferDao().deleteAll();
        db.tripDao().deleteAll();

        db.agencyDao().insertAll(rawToAgencies(data.get(AGENCY_FILE)));
        db.calendarDao().insertAll(rawToCalendars(data.get(CALENDAR_FILE)));
        db.calendarDateDao().insertAll(rawToCalendarDates(data.get(CALENDAR_DATES_FILE)));
        db.feedInfoDao().insertAll(rawToFeedInfos(data.get(FEED_INFO_FILE)));
        db.routeDao().insertAll(rawToRoutes(data.get(ROUTES_FILE)));
        db.tripDao().insertAll(rawToTrips(data.get(TRIPS_FILE)));
        db.stopDao().insertAll(rawToStops(data.get(STOPS_FILE)));
        db.stopTimeDao().insertAll(rawToStopsTimes(data.get(STOP_TIMES_FILE)));
        db.transferDao().insertAll(rawToTransfers(data.get(TRANSFERS_FILE)));

        PreferenceManager.getDefaultSharedPreferences(context).edit().putBoolean(context.getString(R.string.empty), false).apply();
    }
like image 98
gekoramy bean Avatar answered Nov 09 '22 16:11

gekoramy bean