package com.ttstd.dialer.db.app;

import android.content.Context;

import androidx.room.Database;
import androidx.room.Room;
import androidx.room.RoomDatabase;

import java.io.File;

@Database(entities = {AppInfo.class}, version = 1, exportSchema = false)
public abstract class AppDatabase extends RoomDatabase {
    public abstract AppDao appDao();

    private static volatile AppDatabase INSTANCE;

    // 单例模式获取数据库实例
    public static AppDatabase getDatabase(final Context context) {
        if (INSTANCE == null) {
            synchronized (AppDatabase.class) {
                if (INSTANCE == null) {
                    INSTANCE = Room.databaseBuilder(context.getApplicationContext(),
                            AppDatabase.class, context.getExternalFilesDir("db") + File.separator + "app" + File.separator + "app_db")
//                            .allowMainThreadQueries() // 为了简化示例，允许主线程查询
                            .build();
                }
            }
        }
        return INSTANCE;
    }
}
