当前位置: 首页>>代码示例>>Java>>正文


Java Room类代码示例

本文整理汇总了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();
}
 
开发者ID:SergeyVinyar,项目名称:AndroidNewArchitectureExample,代码行数:18,代码来源:AppModule.java

示例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();
}
 
开发者ID:googlesamples,项目名称:android-architecture-components,代码行数:29,代码来源:AppDatabase.java

示例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;
}
 
开发者ID:curbmap,项目名称:curbmap-android,代码行数:24,代码来源:YourContributionsFragment.java

示例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;
}
 
开发者ID:ceanyd,项目名称:react-native-caller-id-android,代码行数:21,代码来源:DataBase.java

示例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;
}
 
开发者ID:goutham106,项目名称:GmArchMvvm,代码行数:22,代码来源:RepositoryManager.java

示例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;
}
 
开发者ID:googlecodelabs,项目名称:android-room-with-a-view,代码行数:17,代码来源:WordRoomDatabase.java

示例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;
}
 
开发者ID:xiaobailong24,项目名称:MVVMArms,代码行数:22,代码来源:RepositoryManager.java

示例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;
    }
 
开发者ID:albertogiunta,项目名称:justintrain-client-android,代码行数:15,代码来源:AppDatabase.java

示例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();
}
 
开发者ID:ponewheel,项目名称:android-ponewheel,代码行数:8,代码来源:App.java

示例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;
}
 
开发者ID:akexorcist,项目名称:Android-ArchComponents,代码行数:9,代码来源:MainActivityViewModel.java

示例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;
}
 
开发者ID:strudelauxpommes,项目名称:FitnessHabits,代码行数:8,代码来源:FitnessHabitsApplication.java

示例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;
}
 
开发者ID:rumaan,项目名称:file.io-app,代码行数:15,代码来源:UploadHistoryRoomDatabase.java

示例13: initializeDb

import android.arch.persistence.room.Room; //导入依赖的package包/类
@Before
public void initializeDb() {
    database = Room.inMemoryDatabaseBuilder(
            InstrumentationRegistry.getContext(),
            UploadHistoryRoomDatabase.class)
            .build();
}
 
开发者ID:rumaan,项目名称:file.io-app,代码行数:8,代码来源:UploadItemDaoTest.java

示例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);
    }

}
 
开发者ID:rumaan,项目名称:file.io-app,代码行数:10,代码来源:UploadHistoryInstrumentedTest.java

示例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;
}
 
开发者ID:feup-infolab,项目名称:labtablet,代码行数:9,代码来源:AppDatabase.java


注:本文中的android.arch.persistence.room.Room类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。