Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

room database,What is the difference between inMemoryDatabaseBuilder and databaseBuilder?

We can get a database throw followed two methods


 /**
 * Copyright:MyApplication
 * Author: liyang <br>
 * Date:2018/6/15 下午5:07<br>
 * Desc: <br>
 */
@Database(entities = {Pet.class ,User.class}, version = 1)
public abstract class RoomDb extends RoomDatabase {
    private static RoomDb INSTANCE;

    private static final Object sLock = new Object();

    public abstract UserDao getUserDao();

    public abstract PetDao getPetDao();

    public static RoomDb getInstance(Context context) {
        if (INSTANCE == null) {
            synchronized (sLock) {
                if (INSTANCE == null) {
                    INSTANCE = Room.databaseBuilder(context.getApplicationContext(),RoomDb.class,"Sample.db").build();
                }
            }
        }
        return INSTANCE;
    }
    public static RoomDb getInMemoreyDatabase(Context context){
        if (INSTANCE == null) {
            synchronized (sLock) {
                if (INSTANCE == null) {
                    INSTANCE = Room.inMemoryDatabaseBuilder(context.getApplicationContext(),RoomDb.class).build();
                }
            }
        }
        return INSTANCE;
    }
}

But I really want to know what the difference is between them! Does getInMemoreyDatabase run faster than Room.databaseBuilder?

like image 582
TaylorLee Avatar asked Jun 16 '18 09:06

TaylorLee


Video Answer


2 Answers

inMemoryDatabaseBuilder(): The database will be created in system memory, If you kill the app (Killing your process), database will be removed and data will not be persisted. This can be used while Testing.

databaseBuilder() : The database will be created in /data/data/com.your.app and will be persisted. This you will be using it in production.

like image 55
Naveen T P Avatar answered Oct 20 '22 22:10

Naveen T P


inMemoryDatabaseBuilder() will build the database on a temporary basis. The database will reside in system memory and it would be destroyed as soon as the process is killed.

databaseBuilder() it will build the database on a permanent basis and the database would be stored in /data/data/.... folder.

like image 38
ankuranurag2 Avatar answered Oct 20 '22 21:10

ankuranurag2