本文整理汇总了Java中android.arch.persistence.room.Room类的典型用法代码示例。如果您正苦于以下问题:Java Room类的具体用法?Java Room怎么用?Java Room使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Room类属于android.arch.persistence.room包,在下文中一共展示了Room类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: provideWeatherDAO
import android.arch.persistence.room.Room; //导入依赖的package包/类
@Provides
@Singleton
WeatherDAO provideWeatherDAO() {
AppDatabase database = Room.databaseBuilder(context, AppDatabase.class, "data")
//.addMigrations(MIGRATION_1_2)
.build();
// If no database found on the device, a new one created via analysing Entity interfaces.
// Migrations aren't executed even when required schema version > 1.
// It means migrations can't be used for populating db with data.
// Hope they change this behaviour.
Executors.newSingleThreadExecutor().execute(() -> {
init(database.weatherDao());
});
return database.weatherDao();
}
示例2: buildDatabase
import android.arch.persistence.room.Room; //导入依赖的package包/类
/**
* Build the database. {@link Builder#build()} only sets up the database configuration and
* creates a new instance of the database.
* The SQLite database is only created when it's accessed for the first time.
*/
private static AppDatabase buildDatabase(final Context appContext,
final AppExecutors executors) {
return Room.databaseBuilder(appContext, AppDatabase.class, DATABASE_NAME)
.addCallback(new Callback() {
@Override
public void onCreate(@NonNull SupportSQLiteDatabase db) {
super.onCreate(db);
executors.diskIO().execute(() -> {
// Add a delay to simulate a long-running operation
addDelay();
// Generate the data for pre-population
AppDatabase database = AppDatabase.getInstance(appContext, executors);
List<ProductEntity> products = DataGenerator.generateProducts();
List<CommentEntity> comments =
DataGenerator.generateCommentsForProducts(products);
insertData(database, products, comments);
// notify that the database was created and it's ready to be used
database.setDatabaseCreated();
});
}
}).build();
}
示例3: onCreateView
import android.arch.persistence.room.Room; //导入依赖的package包/类
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
myView = inflater.inflate(R.layout.your_contributions, container, false);
AppDatabase db = Room.databaseBuilder(myView.getContext(),
AppDatabase.class,
"restrictions").allowMainThreadQueries().build();
RestrictionDao restrictionDao = db.getRestrictionDao();
Log.d(TAG, restrictionDao.getAll().toString());
List<Restriction> listOfRestrictions = restrictionDao.getAll();
//todo: sort by latest entries first
String allCards = "";
for (Restriction restriction : listOfRestrictions) {
allCards += restriction.getCard() + "\n\n";
}
TextView yourContributions = (TextView) myView.findViewById(R.id.yourContributionsText);
yourContributions.setText(allCards);
return myView;
}
示例4: getDatabase
import android.arch.persistence.room.Room; //导入依赖的package包/类
public static DataBase getDatabase(Context context, String passPhrase) {
prefs = context.getSharedPreferences(context.getPackageName(), Context.MODE_PRIVATE);
try {
if (null == INSTANCE) {
if (prefs.getBoolean("first", true) && null != passPhrase) {
createNewKeys(context, getAlias(context));
encryptString_old(context, getAlias(context), passPhrase);
}
String str = decryptString_old(context, getAlias(context));
if(null == str) return null;
SafeHelperFactory factory=SafeHelperFactory.fromUser(Editable.Factory.getInstance().newEditable(str));
INSTANCE = Room.databaseBuilder(context, DataBase.class, "users").openHelperFactory(factory).allowMainThreadQueries().build();
}
} catch (Exception e) {
Log.e(TAG, Log.getStackTraceString(e));
}
return INSTANCE;
}
示例5: obtainRoomDatabase
import android.arch.persistence.room.Room; //导入依赖的package包/类
@Override
public <DB extends RoomDatabase> DB obtainRoomDatabase(Class<DB> database, String dbName) {
if (mRoomDatabaseCache == null) {
mRoomDatabaseCache = mCacheFactory.build(CacheType.ROOM_DATABASE_CACHE_TYPE);
}
Preconditions.checkNotNull(mRoomDatabaseCache, "Cannot return null from a Cache.Factory#build(int) method");
DB roomDatabase;
synchronized (mRoomDatabaseCache) {
roomDatabase = (DB) mRoomDatabaseCache.get(database.getName());
if (roomDatabase == null) {
RoomDatabase.Builder builder = Room.databaseBuilder(mApplication, database, dbName);
//Customize the room configuration
if (mRoomConfiguration != null) {
mRoomConfiguration.configRoom(mApplication, builder);
}
roomDatabase = (DB) builder.build();
mRoomDatabaseCache.put(database.getName(), roomDatabase);
}
}
return roomDatabase;
}
示例6: getDatabase
import android.arch.persistence.room.Room; //导入依赖的package包/类
static WordRoomDatabase getDatabase(final Context context) {
if (INSTANCE == null) {
synchronized (WordRoomDatabase.class) {
if (INSTANCE == null) {
INSTANCE = Room.databaseBuilder(context.getApplicationContext(),
WordRoomDatabase.class, "word_database")
// Wipes and rebuilds instead of migrating if no Migration object.
// Migration is not part of this codelab.
.fallbackToDestructiveMigration()
.addCallback(sRoomDatabaseCallback)
.build();
}
}
}
return INSTANCE;
}
示例7: obtainRoomDatabase
import android.arch.persistence.room.Room; //导入依赖的package包/类
@Override
public <DB extends RoomDatabase> DB obtainRoomDatabase(Class<DB> database, String dbName) {
if (mRoomDatabaseCache == null) {
mRoomDatabaseCache = mCacheFactory.build(CacheType.ROOM_DATABASE_CACHE_TYPE);
}
Preconditions.checkNotNull(mRoomDatabaseCache, "Cannot return null from a Cache.Factory#build(int) method");
DB roomDatabase;
synchronized (mRoomDatabaseCache) {
roomDatabase = (DB) mRoomDatabaseCache.get(database.getName());
if (roomDatabase == null) {
RoomDatabase.Builder builder = Room.databaseBuilder(mApplication, database, dbName);
//自定义 Room 配置
if (mRoomConfiguration != null) {
mRoomConfiguration.configRoom(mApplication, builder);
}
roomDatabase = (DB) builder.build();
mRoomDatabaseCache.put(database.getName(), roomDatabase);
}
}
return roomDatabase;
}
示例8: getAppDatabase
import android.arch.persistence.room.Room; //导入依赖的package包/类
public static AppDatabase getAppDatabase(Context context, boolean force) {
if (INSTANCE == null || force) {
INSTANCE =
Room.databaseBuilder(context.getApplicationContext(), AppDatabase.class, "justintrain.db")
// allow queries on the main thread.
// Don't do\ this on a real app! See PersistenceBasicSample for an example.
.openHelperFactory(new AssetSQLiteOpenHelperFactory())
// .addMigrations(MIGRATION_1_2)
.allowMainThreadQueries()
.build();
}
return INSTANCE;
}
示例9: initDatabase
import android.arch.persistence.room.Room; //导入依赖的package包/类
private void initDatabase() {
db = Room.databaseBuilder(getApplicationContext(),
Database.class, "database-name-pow")
.fallbackToDestructiveMigration()
.build();
dbExecutor = Executors.newSingleThreadExecutor();
}
示例10: getLocationByUser
import android.arch.persistence.room.Room; //导入依赖的package包/类
public LiveData<List<LocationInfo>> getLocationByUser(Context context, String user) {
if (locationInfoList == null) {
LocationInfoDatabase db = Room.databaseBuilder(context,
LocationInfoDatabase.class, LocationDbKey.DATABASE_NAME).build();
locationInfoList = db.locationInfoDao().getLocationByUser(user);
}
return locationInfoList;
}
示例11: getDatabase
import android.arch.persistence.room.Room; //导入依赖的package包/类
@MainThread
public AppDatabase getDatabase() {
if (database == null) {
database = Room.databaseBuilder(this, AppDatabase.class, "FitnessHabits-database").fallbackToDestructiveMigration().build(); //TODO: remove fallback destroy
}
return database;
}
示例12: getInstance
import android.arch.persistence.room.Room; //导入依赖的package包/类
public static UploadHistoryRoomDatabase getInstance(Context context) {
if (INSTANCE == null) {
synchronized (UploadHistoryRoomDatabase.class) {
if (INSTANCE == null) {
INSTANCE = Room.databaseBuilder(context.getApplicationContext(),
UploadHistoryRoomDatabase.class,
"upload_history")
.build();
}
}
}
return INSTANCE;
}
示例13: initializeDb
import android.arch.persistence.room.Room; //导入依赖的package包/类
@Before
public void initializeDb() {
database = Room.inMemoryDatabaseBuilder(
InstrumentationRegistry.getContext(),
UploadHistoryRoomDatabase.class)
.build();
}
示例14: init
import android.arch.persistence.room.Room; //导入依赖的package包/类
@Before
public void init() {
database = Room.inMemoryDatabaseBuilder(InstrumentationRegistry.getContext(), UploadHistoryRoomDatabase.class).build();
for (int i = 0; i < 5; i++) {
UploadItem uploadItem = new UploadItem("test file name " + i, "test URL " + i, Consts.DEFAULT_EXPIRE_WEEKS);
database.uploadItemDao().insert(uploadItem);
}
}
示例15: getDatabase
import android.arch.persistence.room.Room; //导入依赖的package包/类
public static AppDatabase getDatabase(Context context)
{
if(INSTANCE == null)
{
INSTANCE = Room.databaseBuilder(context, AppDatabase.class, "LabTabletDB").build();
}
return INSTANCE;
}