我有一个在我的资源原始文件夹中的域文件。我能够读取对象,更改它们,并为该会话保存它们。但是,当我开始新会话时,数据不会反映我的任何更改。我不会在任何时候调用Realm.deleteRealmFile()或Realm.clear()。
public class MyDb {
private Realm realm;
private RealmConfiguration config0;
private Context context;
public MyDb(Context context) {
this.context = context;
copyBundledRealmFile(context.getResources().openRawResource(R.raw.default0),"default0");
config0 = new RealmConfiguration.Builder(context)
.name("default0")
.build();
realm = Realm.getInstance(config0);
}
private String copyBundledRealmFile(InputStream inputStream, String outFileName) {
try {
File file = new File(context.getFilesDir(), outFileName);
FileOutputStream outputStream = new FileOutputStream(file);
byte[] buf = new byte[1024];
int bytesRead;
while ((bytesRead = inputStream.read(buf)) > 0) {
outputStream.write(buf, 0, bytesRead);
}
outputStream.close();
return file.getAbsolutePath();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public void saveResource(final Resource resourceIn) {
realm.executeTransaction(new Realm.Transaction() {
@Override
public void execute(Realm realm) {
resourceIn.setSaved(true);
}
});
}
public void unSaveResource(final Resource resourceIn) {
realm.executeTransaction(new Realm.Transaction() {
@Override
public void execute(Realm realm) {
resourceIn.setSaved(false);
}
});
}
}