我正在使用realm-android 0.74.0和android 4.4.2 我创建了一个领域实例,用对象填充它并提交事务。 我的物品保存得很好。我可以加载它们并操纵它们。 但每次重新启动我的应用程序时,数据丢失
有没有人遇到同样的问题?
答案 0 :(得分:2)
来自Realm的Christian。
您的登录按钮onClick处理程序具有以下方法:
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
...
// Delete database
Realm.deleteRealmFile(getActivity());
...
}
});
这会删除默认域文件以及您的数据。你试图通过这条线完成其他事情的地方?
答案 1 :(得分:0)
我正在做以下
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_login, container, false);
final Button button = (Button) rootView.findViewById(R.id.connect);
final EditText username = (EditText) rootView.findViewById(R.id.username);
final EditText password = (EditText) rootView.findViewById(R.id.password);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Hide keyboard
InputMethodManager inputManager = (InputMethodManager)
getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(getActivity().getCurrentFocus().getWindowToken(),
InputMethodManager.HIDE_NOT_ALWAYS);
// Call user login service
RestAdapter restAdapter = new RestAdapter.Builder()
.setEndpoint(getString(R.string.app_url_base))
.build();
AppService service = restAdapter.create(AppService.class);
service.login(username.getText().toString(), password.getText().toString(), new Callback<User>() {
@Override
public void success(User user, Response response) {
if (user.isSuccess()) {
SharedPreferences prefs = getActivity().getSharedPreferences(Const.PREFS, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
Log.d(TAG, "Logged username: " + user.getUsername());
editor.putString(getString(R.string.pref_username), user.getUsername());
editor.putString(getString(R.string.pref_password), user.getPassword());
editor.putString(getString(R.string.pref_lastname), user.getLastname());
editor.putString(getString(R.string.pref_firstname), user.getFirstname());
editor.putString(getString(R.string.pref_md5), user.getMd5());
editor.commit();
Toast.makeText(getActivity(), getString(R.string.success_login), Toast.LENGTH_SHORT).show();
// Delete database
Realm.deleteRealmFile(getActivity());
// Delete log file
File logFile = new File(Const.LOG_FILE);
if (logFile.exists()) logFile.delete();
getActivity().getFragmentManager().beginTransaction()
.replace(R.id.container, TasksFragment.newInstance(2, 0))
.commit();
//((MainActivity) getActivity()).openDrawer();
} else {
Toast.makeText(getActivity(), getString(R.string.error_login), Toast.LENGTH_SHORT).show();
}
}
@Override
public void failure(RetrofitError retrofitError) {
Log.d(TAG, "retrofitError:" + retrofitError.getMessage());
Toast.makeText(getActivity(), getString(R.string.error_login), Toast.LENGTH_SHORT).show();
}
});
}
});
return rootView;
}
但是通过移动 Realm.deleteRealmFile(getActivity()); 在改装之前它似乎工作正常
TasksFragment正在运行一个持久化RealmObjects
的服务感谢您的帮助