问题描述
我正在尝试了解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> {
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> {
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> {
private BudgetDao budgetDao;
private DeleteallEntriesAsyncTask(BudgetDao budgetDao) {
this.budgetDao = budgetDao;
}
@Override
protected Void doInBackground(Void... voids) {
budgetDao.deleteallEntries();
return null;
}
}
}
感谢您的帮助!
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)