I have implemented the Room
database in Application. I want to prepopulate the data when app is first opened. The Entity
class looks like
@Entity(tableName = NameConstants.TABLE_NAME)
public class Name {
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@PrimaryKey(autoGenerate = true)
private int id;
@ColumnInfo(name = NameConstants.COL_NAME)
private String Name;
@ColumnInfo(name= NameConstants.COL_PHONE)
private String Phone;
@ColumnInfo(name = NameConstants.COL_CITY)
private String City;
public Name(){
}
public Name(String name, String phone, String city) {
Name = name;
Phone = phone;
City = city;
}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public String getPhone() {
return Phone;
}
public void setPhone(String phone) {
Phone = phone;
}
public String getCity() {
return City;
}
public void setCity(String city) {
City = city;
}
}
The DAO
implementation is
@Dao
public interface NameDAO {
@Insert(onConflict = OnConflictStrategy.REPLACE)
void insertAll(Name... names);
@Insert(onConflict = OnConflictStrategy.REPLACE)
void insert(Name name);
@Delete
void delete(Name name);
}
The Database
class seems to be like this where data is read from one Raw
file
@Database(entities = {Name.class},version = 1,exportSchema = false)
public abstract class NameDatabase extends RoomDatabase {
public abstract NameDAO nameDAO();
private static volatile NameDatabase dInstance=null;
static synchronized NameDatabase getInstance(final Context context){
if(dInstance==null){
synchronized (NameDatabase.class){
if(dInstance==null){
dInstance= Room.databaseBuilder(context.getApplicationContext(),NameDatabase.class,"appdatabase")
.allowMainThreadQueries()
.addCallback(new Callback() {
@Override
public void onCreate(@NonNull SupportSQLiteDatabase db) {
super.onCreate(db);
fillWithDemoData(context);
}
}
).build();
}
}
}
return dInstance;
}
@WorkerThread
private static void fillWithDemoData(Context context){
NameDAO dao=getInstance(context).nameDAO();
JSONArray emoji = loadJsonArray(context);
try {
for (int i = 0; i < emoji.length(); i++) {
JSONObject item = emoji.getJSONObject(i);
dao.insert(new Name(item.getString("name"),
item.getString("phone"),
item.getString("city")));
}
} catch (JSONException exception) {
exception.printStackTrace();
}
}
private static JSONArray loadJsonArray(Context context) {
StringBuilder builder = new StringBuilder();
InputStream in = context.getResources().openRawResource(R.raw.namesdata);
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
String line;
try {
while ((line = reader.readLine()) != null) {
builder.append(line);
}
JSONObject json = new JSONObject(builder.toString());
return json.getJSONArray("names");
} catch (IOException | JSONException exception) {
exception.printStackTrace();
}
return null;
}
}
The Repository
class is public
class DataRepository {
private NameDAO nDao;
public DataRepository(Application application) {
NameDatabase database = NameDatabase.getInstance(application);
nDao = database.nameDAO();
}
}
When app is install and open app works fine, no crash but no Database files are created and no data is inserted in database. What else is missing or anything wrong in code ?