android.arch.persistence.room.Room Java Examples

The following examples show how to use android.arch.persistence.room.Room. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AppDatabase.java    From SABS with MIT License 6 votes vote down vote up
public static AppDatabase getAppDatabase(Context context) {
    if (INSTANCE == null) {
        INSTANCE =
                Room.databaseBuilder(context.getApplicationContext(),
                        AppDatabase.class, "adhell-database")
                        .addMigrations(MIGRATION_14_15)
                        .addMigrations(MIGRATION_15_16)
                        .addMigrations(MIGRATION_16_17)
                        .addMigrations(MIGRATION_17_18)
                        .addMigrations(MIGRATION_18_19)
                        .addMigrations(MIGRATION_19_20)
                        .addMigrations(MIGRATION_20_21)
                        .addMigrations(MIGRATION_21_22)
                        .build();
    }
    return INSTANCE;
}
 
Example #2
Source File: ApplicationDatabase.java    From privacy-friendly-food-tracker with GNU General Public License v3.0 6 votes vote down vote up
public static ApplicationDatabase getInstance(final Context context) throws Exception {
    if (sInstance == null) {
        synchronized (ApplicationDatabase.class) {
            if (sInstance == null) {
                SafeHelperFactory factory = new SafeHelperFactory(KeyGenHelper.getSecretKeyAsChar(context));

                sInstance = Room.databaseBuilder(context.getApplicationContext(),ApplicationDatabase.class, DATABASE_NAME)
                        .openHelperFactory(factory)
                        .allowMainThreadQueries()
                        .addCallback(new Callback() {
                            @Override
                            public void onCreate(@NonNull SupportSQLiteDatabase db) {
                                super.onCreate(db);
                                try {
                                    ApplicationDatabase database = ApplicationDatabase.getInstance(context);
                                    // notify that the database was created and it's ready to be used
                                    database.setDatabaseCreated();
                                } catch (Exception e) {
                                    Log.e("ApplicationDatabase", e.getMessage());
                                }
                            }
                        }).build();

            }
        }
    }
    return sInstance;
}
 
Example #3
Source File: AppDatabase.java    From notSABS with MIT License 6 votes vote down vote up
public static AppDatabase getAppDatabase(Context context) {
    if (INSTANCE == null) {
        INSTANCE =
                Room.databaseBuilder(context.getApplicationContext(),
                        AppDatabase.class, "adhell-database")
                        .addMigrations(MIGRATION_14_15)
                        .addMigrations(MIGRATION_15_16)
                        .addMigrations(MIGRATION_16_17)
                        .addMigrations(MIGRATION_17_18)
                        .addMigrations(MIGRATION_18_19)
                        .addMigrations(MIGRATION_19_20)
                        .addMigrations(MIGRATION_20_21)
                        .addMigrations(MIGRATION_21_22)
                        .build();
    }
    return INSTANCE;
}
 
Example #4
Source File: RepositoryManager.java    From MVVMArms with Apache License 2.0 6 votes vote down vote up
@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;
}
 
Example #5
Source File: ApplicationModule.java    From AndroidBlueprints with Apache License 2.0 5 votes vote down vote up
public ApplicationModule(MyApplication application) {
    this.application = application;

    Timber.i("Build database");
    this.database = Room.databaseBuilder(
            application,
            StarterDatabase.class,
            StarterDatabase.DATABASE_NAME
    ).build();
}
 
Example #6
Source File: ApplicationModule.java    From AndroidBlueprints with Apache License 2.0 5 votes vote down vote up
public ApplicationModule(MyApplication application) {
    this.application = application;

    Timber.i("Build database");
    this.database = Room.databaseBuilder(
            application,
            StarterDatabase.class,
            StarterDatabase.DATABASE_NAME
    ).build();
}
 
Example #7
Source File: CommentDatabase.java    From OfflineSampleApp with Apache License 2.0 5 votes vote down vote up
public static synchronized CommentDatabase getInstance(Context context) {
    if (instance == null) {
        instance = Room
                .databaseBuilder(context.getApplicationContext(), CommentDatabase.class, ModelConstants.DB_NAME)
                .build();
    }
    return instance;
}
 
Example #8
Source File: AppDatabase.java    From Expert-Android-Programming with MIT License 5 votes vote down vote up
public static AppDatabase getDatabase(Context context) {
    if (INSTANCE == null) {
        INSTANCE = Room.databaseBuilder(context.getApplicationContext(), AppDatabase.class, "zomato_room_db")
                .build();
    }
    return INSTANCE;
}
 
Example #9
Source File: UserRepository.java    From OpenYOLO-Android with Apache License 2.0 5 votes vote down vote up
/**
 * Creates the user repository, with the required application reference.
 */
public UserRepository(@NonNull OpenYoloDemoApplication application) {
    mUserDatabase = Room.databaseBuilder(application, UserDatabase.class, USER_DB_NAME)
            .build();
    mSharedPrefs = application.getSharedPreferences(
            AUTH_INFO_SHARED_PREFS,
            Context.MODE_PRIVATE);
}
 
Example #10
Source File: DatabaseCreator.java    From AndroidRoom with GNU General Public License v3.0 5 votes vote down vote up
public synchronized static PersonDatabase getPersonDatabase(Context context){
    if(personDatabase == null) {
        synchronized (LOCK) {
            if (personDatabase == null) {
                personDatabase = Room.databaseBuilder(context,
                        PersonDatabase.class, "person db").build();
            }
        }
    }
    return personDatabase;
}
 
Example #11
Source File: AppDataBase.java    From Android-AudioRecorder-App with Apache License 2.0 5 votes vote down vote up
public static AppDataBase getInstance(Context context) {
  if (appDataBaseInstance == null) {
    appDataBaseInstance = Room.databaseBuilder(context, AppDataBase.class, DATABASE_NAME).build();
    appDataBaseInstance.recordItemDataSource =
        new RecordItemDataSource(appDataBaseInstance.recordItemDao());
  } return appDataBaseInstance;
}
 
Example #12
Source File: App.java    From android-ponewheel with MIT License 5 votes vote down vote up
private void initDatabase() {
    db = Room.databaseBuilder(getApplicationContext(),
            Database.class, "database-name-pow")
            .fallbackToDestructiveMigration()
            .build();
    dbExecutor = Executors.newSingleThreadExecutor();
}
 
Example #13
Source File: GSoCApp.java    From GSoC-Info-Android with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void onCreate() {
    super.onCreate();
    appDatabase = Room.databaseBuilder(getApplicationContext(), AppDatabase.class, "gsoc-database")
            .fallbackToDestructiveMigration()
            .build();
}
 
Example #14
Source File: EarthquakeDatabaseAccessor.java    From Wrox-ProfessionalAndroid-4E with Apache License 2.0 5 votes vote down vote up
public static EarthquakeDatabase getInstance(Context context) {
  if (EarthquakeDatabaseInstance == null) {
    // Create or open a new SQLite database, and return it as
    // a Room Database instance.
    EarthquakeDatabaseInstance = Room.databaseBuilder(context,
      EarthquakeDatabase.class, EARTHQUAKE_DB_NAME).build();
  }

  return EarthquakeDatabaseInstance;
}
 
Example #15
Source File: WaitDataBase.java    From Readhub with Apache License 2.0 5 votes vote down vote up
public static WaitDataBase getDatabase() {
    if (sInstance == null) {
        sInstance = Room.databaseBuilder(ReadhubApplication.getInstance(), WaitDataBase.class,
                "Readhub").build();
    }
    return sInstance;
}
 
Example #16
Source File: App.java    From RoomDemo with Apache License 2.0 5 votes vote down vote up
@Override
public void onCreate() {
    super.onCreate();

    // create database
    database = Room.databaseBuilder(getApplicationContext(), MyDatabase.class, DATABASE_NAME)
            .addMigrations(MyDatabase.MIGRATION_1_2)
            .build();

    INSTANCE = this;
}
 
Example #17
Source File: AppDatabase.java    From android-persistence with Apache License 2.0 5 votes vote down vote up
public static AppDatabase getInMemoryDatabase(Context context) {
    if (INSTANCE == null) {
        INSTANCE =
                Room.inMemoryDatabaseBuilder(context.getApplicationContext(), AppDatabase.class)
                // To simplify the codelab, allow queries on the main thread.
                // Don't do this on a real app! See PersistenceBasicSample for an example.
                .allowMainThreadQueries()
                .build();
    }
    return INSTANCE;
}
 
Example #18
Source File: BitsharesApplication.java    From bitshares_wallet with MIT License 5 votes vote down vote up
@Override
public void onCreate() {
    super.onCreate();
    Fabric.with(this, new Crashlytics());
    Security.insertProviderAt(new BouncyCastleProvider(), 1);

    bitsharesDatabase = Room.databaseBuilder(
            this,
            BitsharesDatabase.class,
            "bitshares.db"
    ).build();

    // 注册回调,保证数据更新
}
 
Example #19
Source File: AppDatabase.java    From SqliteManager with Apache License 2.0 5 votes vote down vote up
public static AppDatabase getInMemoryDatabase(Context context) {
    if (IN_MEMORY_INSTANCE == null) {
        IN_MEMORY_INSTANCE =
                Room.inMemoryDatabaseBuilder(context.getApplicationContext(), AppDatabase.class)
                        // To simplify the codelab, allow queries on the main thread.
                        // Don't do this on a real app! See PersistenceBasicSample for an example.
                        .allowMainThreadQueries()
                        .build();
    }
    return IN_MEMORY_INSTANCE;
}
 
Example #20
Source File: AutofillDaoTest.java    From android-AutofillFramework with Apache License 2.0 5 votes vote down vote up
@Before
public void setup() {
    // using an in-memory database because the information stored here disappears when the
    // process is killed
    mDatabase = Room.inMemoryDatabaseBuilder(InstrumentationRegistry.getContext(),
            AutofillDatabase.class).build();

}
 
Example #21
Source File: NewsDatabase.java    From NewsApp with GNU General Public License v3.0 5 votes vote down vote up
public static NewsDatabase getInstance(Context context) {
    if (sInstance == null) {
        synchronized (LOCK) {
            sInstance = Room.databaseBuilder(
                    context.getApplicationContext(),
                    NewsDatabase.class,
                    DATABASE_NAME).build();
        }
    }
    return sInstance;
}
 
Example #22
Source File: ToDoDatabase.java    From android-espresso-revealed with Apache License 2.0 5 votes vote down vote up
public static ToDoDatabase getInstance(Context context) {
    synchronized (sLock) {
        if (INSTANCE == null) {
            INSTANCE = Room.databaseBuilder(context.getApplicationContext(),
                    ToDoDatabase.class, "Tasks.db")
                    .build();
        }
        return INSTANCE;
    }
}
 
Example #23
Source File: EarthquakeDatabaseAccessor.java    From Wrox-ProfessionalAndroid-4E with Apache License 2.0 5 votes vote down vote up
public static EarthquakeDatabase getInstance(Context context) {
  if (EarthquakeDatabaseInstance == null) {
    // Create or open a new SQLite database, and return it as
    // a Room Database instance.
    EarthquakeDatabaseInstance = Room.databaseBuilder(context,
      EarthquakeDatabase.class, EARTHQUAKE_DB_NAME).build();
  }

  return EarthquakeDatabaseInstance;
}
 
Example #24
Source File: EarthquakeDatabaseAccessor.java    From Wrox-ProfessionalAndroid-4E with Apache License 2.0 5 votes vote down vote up
public static EarthquakeDatabase getInstance(Context context) {
  if (EarthquakeDatabaseInstance == null) {
    // Create or open a new SQLite database, and return it as
    // a Room Database instance.
    EarthquakeDatabaseInstance = Room.databaseBuilder(context,
      EarthquakeDatabase.class, EARTHQUAKE_DB_NAME).build();
  }

  return EarthquakeDatabaseInstance;
}
 
Example #25
Source File: InfoDB.java    From Tok-Android with GNU General Public License v3.0 5 votes vote down vote up
public static InfoDB getInstance(Context context) {
    synchronized (sLock) {
        String dbName = PreferenceUtils.getAccount();
        if (INSTANCE == null) {
            INSTANCE =
                Room.databaseBuilder(context.getApplicationContext(), InfoDB.class, dbName)
                    .allowMainThreadQueries()
                    .addMigrations(MIGRATION_32_33, MIGRATION_33_34, MIGRATION_34_35)
                    .build();
        }
        return INSTANCE;
    }
}
 
Example #26
Source File: EarthquakeDatabaseAccessor.java    From Wrox-ProfessionalAndroid-4E with Apache License 2.0 5 votes vote down vote up
public static EarthquakeDatabase getInstance(Context context) {
  if (EarthquakeDatabaseInstance == null) {
    // Create or open a new SQLite database, and return it as
    // a Room Database instance.
    EarthquakeDatabaseInstance = Room.databaseBuilder(context,
      EarthquakeDatabase.class, EARTHQUAKE_DB_NAME).build();
  }

  return EarthquakeDatabaseInstance;
}
 
Example #27
Source File: EarthquakeDatabaseAccessor.java    From Wrox-ProfessionalAndroid-4E with Apache License 2.0 5 votes vote down vote up
public static EarthquakeDatabase getInstance(Context context) {
  if (EarthquakeDatabaseInstance == null) {
    // Create or open a new SQLite database, and return it as
    // a Room Database instance.
    EarthquakeDatabaseInstance = Room.databaseBuilder(context,
      EarthquakeDatabase.class, EARTHQUAKE_DB_NAME).build();
  }

  return EarthquakeDatabaseInstance;
}
 
Example #28
Source File: EarthquakeDatabaseAccessor.java    From Wrox-ProfessionalAndroid-4E with Apache License 2.0 5 votes vote down vote up
public static EarthquakeDatabase getInstance(Context context) {
  if (EarthquakeDatabaseInstance == null) {
    // Create or open a new SQLite database, and return it as
    // a Room Database instance.
    EarthquakeDatabaseInstance = Room.databaseBuilder(context,
      EarthquakeDatabase.class, EARTHQUAKE_DB_NAME).build();
  }

  return EarthquakeDatabaseInstance;
}
 
Example #29
Source File: EarthquakeDatabaseAccessor.java    From Wrox-ProfessionalAndroid-4E with Apache License 2.0 5 votes vote down vote up
public static EarthquakeDatabase getInstance(Context context) {
  if (EarthquakeDatabaseInstance == null) {
    // Create or open a new SQLite database, and return it as
    // a Room Database instance.
    EarthquakeDatabaseInstance = Room.databaseBuilder(context,
      EarthquakeDatabase.class, EARTHQUAKE_DB_NAME).build();
  }

  return EarthquakeDatabaseInstance;
}
 
Example #30
Source File: EarthquakeDatabaseAccessor.java    From Wrox-ProfessionalAndroid-4E with Apache License 2.0 5 votes vote down vote up
public static EarthquakeDatabase getInstance(Context context) {
  if (EarthquakeDatabaseInstance == null) {
    // Create or open a new SQLite database, and return it as
    // a Room Database instance.
    EarthquakeDatabaseInstance = Room.databaseBuilder(context,
      EarthquakeDatabase.class, EARTHQUAKE_DB_NAME).build();
  }

  return EarthquakeDatabaseInstance;
}