Pre populating the database with agents and agencies
This commit is contained in:
parent
2a8d8063c1
commit
00830fb0d1
@ -1,8 +1,15 @@
|
||||
package fr.romanet.vj.apps.myrealestateagency.database;
|
||||
|
||||
import android.content.Context;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.room.Database;
|
||||
import androidx.room.Room;
|
||||
import androidx.room.RoomDatabase;
|
||||
import androidx.room.TypeConverters;
|
||||
import androidx.sqlite.db.SupportSQLiteDatabase;
|
||||
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
import fr.romanet.vj.apps.myrealestateagency.dao.AgencyDao;
|
||||
import fr.romanet.vj.apps.myrealestateagency.dao.AgencyWithAgentsDao;
|
||||
@ -28,4 +35,32 @@ public abstract class RealEstateAgencyDatabase extends RoomDatabase {
|
||||
public abstract AgencyWithPropertiesDao agencyWithPropertiesDaoDao();
|
||||
public abstract DollarCurrencyDao dollarCurrencyDao();
|
||||
|
||||
private static RealEstateAgencyDatabase INSTANCE;
|
||||
|
||||
public synchronized static RealEstateAgencyDatabase getInstance(Context context) {
|
||||
if (INSTANCE == null) {
|
||||
INSTANCE = buildDatabase(context);
|
||||
}
|
||||
return INSTANCE;
|
||||
}
|
||||
|
||||
private static RealEstateAgencyDatabase buildDatabase(final Context context) {
|
||||
return Room.databaseBuilder(context,
|
||||
RealEstateAgencyDatabase.class,
|
||||
"my-real_estate_agency_db")
|
||||
.addCallback(new Callback() {
|
||||
@Override
|
||||
public void onCreate(@NonNull SupportSQLiteDatabase db) {
|
||||
super.onCreate(db);
|
||||
Executors.newSingleThreadScheduledExecutor().execute(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
getInstance(context).agencyDao().insertPreData(Agency.populateAgencyTable());
|
||||
getInstance(context).agentDao().insertPreAgents(Agent.populateAgentsTable());
|
||||
}
|
||||
});
|
||||
}
|
||||
})
|
||||
.allowMainThreadQueries().build();
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user