在我的应用程序启动画面中,我运行一个Thread来填充数据库,但每次打开应用程序时它都会用相同的项填充数据库,我不想放置像isFirstTime
这样的布尔标志。在填充数据库之后将其设置为false,是否有其他解决方案?
这是我的实体类:
@Entity
public class Home {
public @PrimaryKey(autoGenerate = true)
int id;
private int homeCode;
private String homeName;
public Bank(int homeCode, String homeName) {
this.homeCode = homeCode;
this.homeName = homeName;
}
int getHomeCode() {
return homeCode;
}
public void setHomeCode(int homeCode) {
this.homeCode = homeCode;
}
....
}
你觉得autogenerating the primary key
是个问题吗?也许房间认为它有不同的项目,因为他们的主键是不同的?
答案 0 :(得分:0)
没有自动生成主键不是创建重复项的问题。 我提供了一种使用方式,我认为解决了你的问题。 为下面的扩展应用程序创建一个类...
public class AppActivity extends Application {
static AppDatabase db;
@Override
public void onCreate() {
super.onCreate();
db = Room.databaseBuilder(getApplicationContext(), AppDatabase.class, "database-name").build();
}
public static AppDatabase getDatabase() {
return db;
}
}
此类调用applcation标记中的mainfest文件,如下所示。
android:name=".db.AppActivity" // application tag in.
答案 1 :(得分:0)
纠正我,如果这是一个不好的做法,但在我填充数据库之前我删除它以防万一,因为onConflict策略不能以某种方式工作
new Thread(() -> database.bankDao().deleteAllBanks());
答案 2 :(得分:0)
您可以继续 onCreate callback。第一次创建数据库时会调用它。实例化房间时,只需拨打 addCallback 即可。
示例:
@Provides
@Singleton
AppDatabase provideDatabase(Application application) {
return Room
.databaseBuilder(application, AppDatabase.class, BuildConfig.DB_NAME)
.addCallback(new RoomDatabase.Callback() {
@Override
public void onCreate(@NonNull SupportSQLiteDatabase db) {
super.onCreate(db);
ContentValues contentValues = new ContentValues();
contentValues.put("title", "Test 1");
contentValues.put("content", "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec qu");
contentValues.put("type", 0);
contentValues.put("date", 1525421352);
db.insert(NotificationEntity.TABLE_NAME, OnConflictStrategy.IGNORE, contentValues);
contentValues.put("title", "Test 2");
contentValues.put("content", "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec.");
contentValues.put("type", 1);
contentValues.put("date", 1225421242);
db.insert(NotificationEntity.TABLE_NAME, OnConflictStrategy.IGNORE, contentValues);
contentValues.put("title", "Test 3");
contentValues.put("content", "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a");
contentValues.put("type", 2);
contentValues.put("date", 1525000242);
db.insert(NotificationEntity.TABLE_NAME, OnConflictStrategy.IGNORE, contentValues);
contentValues.put("title", "Test 4");
contentValues.put("content", "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium. Integer tincidunt. Cras dapibu");
contentValues.put("type", 3);
contentValues.put("date", 1520420000);
db.insert(NotificationEntity.TABLE_NAME, OnConflictStrategy.IGNORE, contentValues);
}
})
.build();
}