Android如何备份会议室数据库

时间:2020-10-21 19:13:06

标签: android database android-room

我正在尝试了解Google如何备份Room数据库的数据。根据文档,它应该将内容保存在getDatabasePath()中,并且我的数据库在那里。当前,每次我卸载我的App时,房间数据库都会被清除(使用模拟器登录并在设置中备份)。我的目标是备份数据,以便在用户重新安装时保留数据。我将其添加到清单中,没有任何改变:

android:allowBackup="true"

我看到其他人和我有相反的问题,不确定我在做什么错。我需要让用户在应用程序本身上使用其Google帐户登录才能使此功能正常工作吗?也许我在一起误会了。

数据库:

@Database(entities = {BudgetEntry.class}, version = 1, exportSchema = false)

公共抽象类AppDatabase扩展了RoomDatabase {

private static AppDatabase INSTANCE;

public abstract BudgetDao budgetDao();

public static synchronized AppDatabase getDatabase(Context context) {
    if (INSTANCE == null) {
        INSTANCE =
                Room.databaseBuilder(context.getApplicationContext(), AppDatabase.class, "budget_database")

                        .fallbackToDestructiveMigration()
                        .addCallback(roomCallback)
                        .build();
    }
    return INSTANCE;
}

private static RoomDatabase.Callback roomCallback = new RoomDatabase.Callback() {

    @Override
    public void onCreate(@NonNull SupportSQLiteDatabase db) {
        super.onCreate(db);
        new PopulateDatabaseAsyncTask(INSTANCE).execute();
    }
};

private static class PopulateDatabaseAsyncTask extends AsyncTask<Void, Void, Void> {

    private BudgetDao budgetDao;

    private PopulateDatabaseAsyncTask(AppDatabase appDatabase) {
        budgetDao = appDatabase.budgetDao();
    }

    //Example Add
    @Override
    protected Void doInBackground(Void... voids) {
        return null;
    }
}

}

存储库:

public class BudgetRepository {

private BudgetDao budgetDao;
private LiveData<List<BudgetEntry>> allEntries;

public BudgetRepository(Application application) {

    AppDatabase database = AppDatabase.getDatabase(application);
    budgetDao = database.budgetDao();
    allEntries = budgetDao.getAllEntries();

}

public void insert(BudgetEntry budgetEntry) {

    new InsertBudgetAsyncTask(budgetDao).execute(budgetEntry);
}


public void delete(BudgetEntry budgetEntry) {

    new DeleteBudgetAsyncTask(budgetDao).execute(budgetEntry);
}

public void deleteAllEntries() {

    new DeleteAllEntriesAsyncTask(budgetDao).execute();

}

public LiveData<List<BudgetEntry>> getAllEntries() {
 return allEntries;
}

public LiveData<Integer> getCount() {
    return budgetDao.getCount();
}

private static class InsertBudgetAsyncTask extends AsyncTask<BudgetEntry, Void, Void> {
    private BudgetDao budgetDao;

    private InsertBudgetAsyncTask(BudgetDao budgetDao) {
        this.budgetDao = budgetDao;
    }

    @Override
    protected Void doInBackground(BudgetEntry... budgetEntries) {
        budgetDao.addBudgetData(budgetEntries[0]);
        return null;
    }
}

private static class DeleteBudgetAsyncTask extends AsyncTask<BudgetEntry, Void, Void> {
    private BudgetDao budgetDao;

    private DeleteBudgetAsyncTask(BudgetDao budgetDao) {
        this.budgetDao = budgetDao;
    }

    @Override
    protected Void doInBackground(BudgetEntry... budgetEntries) {
        budgetDao.delete(budgetEntries[0]);
        return null;
    }
}

private static class DeleteAllEntriesAsyncTask extends AsyncTask<Void, Void, Void> {
    private BudgetDao budgetDao;

    private DeleteAllEntriesAsyncTask(BudgetDao budgetDao) {
        this.budgetDao = budgetDao;
    }

    @Override
    protected Void doInBackground(Void... voids) {
        budgetDao.deleteAllEntries();
        return null;
    }
}

}

感谢您的帮助!

0 个答案:

没有答案