从领域数据库删除数据时发生运行时异常
有时候这个异常,有时候不是,我使用findAll删除数据 有时数据会被推送到api,然后删除,但有时会显示此异常
消息列表
public class messagelist extends AppCompatActivity {
private static final String TAG = "messagelist";
private RecyclerView recyclerView;
private ActivityMessagelistBinding activityMessagelistBinding;
private Postmessagemodel postmessagemodel;
private RecyclerView.Adapter adapter;
private LinearLayoutManager linearLayoutManager;
private DividerItemDecoration dividerItemDecoration;
private messageadapter customAdapter;
private messagelist_datamanager dataManger;
private postmessage_datamanager postmessage_datamanager;
private List<messagemodel> newsList;
private offlinemsg off;
final Handler handler = new Handler();
Timer timer1;
TimerTask dotask1;
String tokens;
String sendmsg;
Realm realm;
String db_msg, db_auto;
int db_touserid;
int toid;
private TimerTask doTask;
private Timer timer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_messagelist);
toid = getIntent().getExtras().getInt("clickid");
Log.e("toid", String.valueOf(toid));
SharedPreferences pref = getSharedPreferences("MyPrefs", Context.MODE_PRIVATE);
tokens = pref.getString("sherdtoken", "");
realm = Realm.getDefaultInstance();
activityMessagelistBinding = DataBindingUtil.setContentView(this, R.layout.activity_messagelist);
recyclerView = (RecyclerView) findViewById(R.id.recycle1);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
newsList = new ArrayList<>();
customAdapter = new messageadapter(this, newsList);
recyclerView.setAdapter(customAdapter);
postmessagemodel = new Postmessagemodel();
postmessage_datamanager = new postmessage_datamanager(this);
activityMessagelistBinding.setPostmsg(postmessagemodel);
dataManger = new messagelist_datamanager(this);
activityMessagelistBinding.setPostbtn(new Post() {
@Override
public void onclick() {
sendmsg = postmessagemodel.getMSG();
Log.e("token", tokens + "message" + postmessagemodel.getMSG());
if (isNetworkAvailable() && sendmsg != null) {
postmessage(sendmsg, toid, tokens);
} else {
writedb(sendmsg.toString(), tokens.toString(), toid);
}
}
});
if(isNetworkAvailable()){
RealmResults<offlinemsg> allofflinemsg = realm.where(offlinemsg.class).findAll();
if(allofflinemsg != null)
{
for (offlinemsg f : allofflinemsg) {
db_msg = f.getOfflinemsg_msg();
db_auto = f.getOfflinemsg_auth();
db_touserid = f.getOfflinemsg_touserid();
offlinepostmessage(db_msg, db_touserid, db_auto);
Log.e("db_msg",db_msg+" db_auth"+db_auto+" db_touserid"+db_touserid);
}
}
}
getmessage();
/* timer = new Timer();
doTask = new TimerTask() {
@Override
public void run() {
handler.post(new Runnable() {
@SuppressLint("WrongConstant")
@SuppressWarnings("unchecked")
public void run() {
try {
if (isNetworkAvailable()) {
Log.e("connected", String.valueOf(true));
} else {
Toast.makeText(getApplicationContext(), "wait for app goes online to get message after refresh", 5000).show();
}
Intent intent = getIntent();
finish();
startActivity(intent);
} catch (Exception e) {
// TODO Auto-generated catch block
}
}
});
}
};
timer.schedule(doTask, 20000);*/
}
@Override
public void onBackPressed() {
super.onBackPressed();
// timer.cancel();
//doTask.cancel();
Intent intent = new Intent(messagelist.this, userlist.class);
startActivity(intent);
messagelist.this.finish();
}
private void deleteofflinedata(final int db_toid)
{
realm.executeTransaction(new Realm.Transaction() {
@Override
public void execute(Realm realm) {
try{
RealmResults<offlinemsg> result = realm.where(offlinemsg.class).equalTo("Offlinemsg_touserid",db_toid).findAll();
result.deleteAllFromRealm();
}
catch (Exception e)
{
}
}
});
}
private void writedb(final String sendmsg, final String tokens, final int toid)
{
realm.executeTransactionAsync(new Realm.Transaction()
{
@Override
public void execute(Realm bgRealm) {
Number nextid = bgRealm.where(offlinemsg.class).max("offlinemsg_id");
int newkey = (nextid == null) ? 1 : nextid.intValue()+1;
offlinemsg off_msg = bgRealm.createObject(offlinemsg.class,newkey);
off_msg.setOfflinemsg_msg(sendmsg);
off_msg.setOfflinemsg_auth(tokens);
off_msg.setOfflinemsg_touserid(toid);
}
},
new Realm.Transaction.OnSuccess() {
@Override
public void onSuccess()
{
Log.v("Database", "data enterd");
}
},
new Realm.Transaction.OnError()
{
@Override
public void onError(Throwable error) {
Log.e("error", error.getMessage());
}
});
}
private boolean isNetworkAvailable() //check app is online or not
{
ConnectivityManager manager =
(ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = manager.getActiveNetworkInfo();
boolean isAvailable = false;
if (networkInfo != null && networkInfo.isConnected()) {
// Network is present and connected
isAvailable = true;
}
return isAvailable;
}
public void getmessage() {
dataManger.sendVolleyRequest2(tokens, toid, messagelist.this, new messagelist_datavalue() {
@Override
public void setJsonDataResponse(JSONArray response) {
try {
messagemodel userModel = new messagemodel();
for (int i = 0; i < response.length(); i++) {
JSONObject jsonObject = response.getJSONObject(i);
// Log.e("final", String.valueOf(i));
userModel.setFromUserId(jsonObject.getInt("fromUserId"));
userModel.setMessage(jsonObject.getString("message"));
userModel.setToUserId(jsonObject.getInt("toUserId"));
Log.e("message", userModel.getMessage());
newsList.add(userModel);
}
} catch (JSONException jsonDataResponse) {
Log.e("error", String.valueOf(jsonDataResponse));
}
customAdapter.notifyDataSetChanged();
}
@Override
public void setVolleyError(VolleyError volleyError) {
Log.e("Volley", volleyError.toString());
}
});
}
private void postmessage(String post_msg, int post_toid, String post_auth) {
postmessage_datamanager.sendVolleyRequest3(post_msg, post_toid, post_auth, messagelist.this, new postmessage_datavalue() {
@Override
public void setJsonDataResponse(JSONObject response) {
}
@Override
public void setVolleyError(VolleyError volleyError) {
}
});
}
private void offlinepostmessage(String post_msg1, int post_toid1, String post_auth1) {
postmessage_datamanager.sendVolleyRequest3(post_msg1, post_toid1, post_auth1, messagelist.this, new postmessage_datavalue() {
@Override
public void setJsonDataResponse(JSONObject response) {
}
@Override
public void setVolleyError(VolleyError volleyError) {
}
});
deleteofflinedata(post_toid1);
}
@Override
protected void onDestroy() {
super.onDestroy();
realm.close();
}
}
offlinemsg领域模型
public class offlinemsg extends RealmObject {
@PrimaryKey
private int offlinemsg_id;
private String Offlinemsg_msg;
private String Offlinemsg_auth;
private int Offlinemsg_touserid;
public int getOfflinemsg_id() {
return offlinemsg_id;
}
public void setOfflinemsg_id(int offlinemsg_id) {
this.offlinemsg_id = offlinemsg_id;
}
public String getOfflinemsg_msg() {
return Offlinemsg_msg;
}
public void setOfflinemsg_msg(String offlinemsg_msg) {
Offlinemsg_msg = offlinemsg_msg;
// Log.e("Offlinemsg_msg",Offlinemsg_msg);
}
public String getOfflinemsg_auth() {
return Offlinemsg_auth;
}
public void setOfflinemsg_auth(String offlinemsg_auth) {
Offlinemsg_auth = offlinemsg_auth;
// Log.e("Offlinemsg_auth",Offlinemsg_auth);
}
public int getOfflinemsg_touserid() {
return Offlinemsg_touserid;
}
public void setOfflinemsg_touserid(int offlinemsg_touserid) {
Offlinemsg_touserid = offlinemsg_touserid;
// Log.e("Offlinemsg_touserid", String.valueOf(Offlinemsg_touserid));
}
}
当我从数据库删除数据时,就会出现此错误
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.mayurpancholi.chat_mvvm, PID: 9319
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mayurpancholi.chat_mvvm/com.example.mayurpancholi.chat_mvvm.messagelist}: java.lang.IllegalStateException: Object is no longer valid to operate on. Was it deleted by another thread?
它有时会起作用