Skip to content

Instantly share code, notes, and snippets.

@mutairibassam
Last active March 30, 2021 05:21
Show Gist options
  • Save mutairibassam/c6caf87bca2bd7d5e90ffba827b1d9ec to your computer and use it in GitHub Desktop.
Save mutairibassam/c6caf87bca2bd7d5e90ffba827b1d9ec to your computer and use it in GitHub Desktop.
public abstract TodoDao todoDao();
//Create the WordRoomDatabase as a singleton to prevent having multiple instances of the database opened
//at the same time, which would be a bad thing
private static TodoDatabase INSTANCE;
public static final String DATABASE_NAME = "TASK_DATABASE";
// singlton: to make sure there is no more than one copy
// @synchronized one thread only can deal with this database
public static synchronized TodoDatabase getInstance(Context context) {
if(INSTANCE == null) {
INSTANCE = Room.databaseBuilder(context.getApplicationContext(),
TodoDatabase.class, DATABASE_NAME)
.fallbackToDestructiveMigration()
.build();
}
return INSTANCE;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment