android - Realm exception "nested transaction not allowed" even though there are no nested transactions -
recently started android app project. , needed manage data structured possible.
so select realm , tinker it.
but faced error. don't know why error happens.
the error
nested transactions not allowed. use committransaction() after each begintransaction().
my code in below.
public class curatorapplication extends application { private realm realm; @override public void oncreate() { super.oncreate(); log.e("debug", "application class create!!"); configurerealmdatabase(this); } private void configurerealmdatabase(context context){ realmconfiguration config = new realmconfiguration.builder(context) .name("curator.realm") .build(); realm.setdefaultconfiguration(config); } }
i register realm @ application class described in here
and tried transaction @ activity. shows error. :(
package com.nolgong.curator.screen; import android.os.bundle; import android.support.v7.app.appcompatactivity; import android.util.log; import android.view.view; import android.widget.button; import android.widget.edittext; import com.nolgong.curator.r; import com.nolgong.curator.model.retrofit.game; import com.nolgong.curator.model.retrofit.gameinformation; import com.nolgong.curator.model.retrofit.team; import com.nolgong.curator.network.networkclient; import java.io.ioexception; import java.util.properties; import io.realm.realm; import io.realm.exceptions.realmprimarykeyconstraintexception; import retrofit2.call; import retrofit2.callback; import retrofit2.response; public class introactivity extends appcompatactivity { private button confirmbtn; private edittext confirmtext; private realm realm; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_intro); setup(); registerlistener(); } private string getproperty() throws ioexception{ properties properties = new properties(); properties.load(getresources().openrawresource(r.raw.config)); string property = properties.getproperty("serveraddress"); log.e("debug", "property : " + property); return property; } private void setnetworkclient(string serveraddress){ log.e("debug", "address : " + serveraddress); networkclient.getinstance(serveraddress); } private void setup(){ try { setnetworkclient(getproperty()); } catch (ioexception e){ log.e("debug", "set network" + e); } confirmbtn = (button)findviewbyid(r.id.intro_confirm); confirmtext = (edittext)findviewbyid(r.id.intro_input); realm = realm.getdefaultinstance(); log.e("debug", "transaction state : " + realm.isintransaction()); log.e("debug", "configuration : \n" + realm.getconfiguration()); } private void registerlistener(){ confirmbtn.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { string teamid = confirmtext.gettext().tostring(); integer digit = integer.valueof(teamid); log.e("debug", digit + ""); networkclient.getinstance().login(digit, new callback<gameinformation>() { @override public void onresponse(call<gameinformation> call, response<gameinformation> response) { int responsecode = response.code(); switch (responsecode){ case 200: gameinformation gameinformation = response.body(); log.e("debug", "game information " + gameinformation.tostring()); game game = gameinformation.getgame(); team team = gameinformation.getteam(); updategametorealm(game); updateteamtorealm(team); break; default: log.e("debug", "maybe happened."); break; } } @override public void onfailure(call<gameinformation> call, throwable t) { log.e("debug", "login fail :" + t.tostring()); } }); } }); } private void updategametorealm(game game){ com.nolgong.curator.model.database.game rgame = new com.nolgong. curator.model.database.game(game.getid(), game.getdate(), game.getsession(), game.getrunningtime()); realm.begintransaction(); try { realm.copytorealm(rgame); } catch (realmprimarykeyconstraintexception e){ log.e("debug", e.tostring()); realm.canceltransaction(); } { realm.committransaction(); } } private void updateteamtorealm(team team){ com.nolgong.curator.model.database.team rteam = new com.nolgong. curator.model.database.team(team.getid(), team.getmembers(), team.getgameid(), team.isclientdatasynced(), team.getjob(), team.getdigit(), team.getpoint()); realm.begintransaction(); try { realm.copytorealm(rteam); } catch (realmprimarykeyconstraintexception e){ log.e("debug", e.tostring()); realm.canceltransaction(); } { realm.committransaction(); } } @override protected void ondestroy() { super.ondestroy(); realm.close(); } }
why realm shows error? did use correctly? or it's bug? please me ioi..
as error says,
nested transactions not allowed. use committransaction() after each begintransaction().
it means can't this:
realm.begintransaction(); ... realm.begintransaction(); realm.committransaction();
a begintransaction()
call must followed either committransaction()
or canceltransaction()
call.
it's highly recommended use executetransaction()
instead of begin/cancel/commit
, because it's easier use, , handles cancel on exceptions automatically.
edit: should not commit transaction after it's been rolled canceltransaction()
.
please try replacing begin/cancel/commit
executetransaction()
, see happens.
also, can try replacing copytorealm()
copytorealmorupdate()
.
i think might running this issue because of multiple transactions on ui thread ran failure, i'm not sure that.
edit2:
private void updategametorealm(game game){ realm realm = null; try { realm = realm.getdefaultinstance(); final com.nolgong.curator.model.database.game rgame = new com.nolgong. curator.model.database.game(game.getid(), game.getdate(), game.getsession(), game.getrunningtime()); realm.executetransaction(new realm.transaction() { @override public void execute(realm realm) { realm.copytorealmorupdate(rgame); } }); } { if(realm != null) { realm.close(); } } } private void updateteamtorealm(team team){ realm realm = null; try { realm = realm.getdefaultinstance(); final com.nolgong.curator.model.database.team rteam = new com.nolgong. curator.model.database.team(team.getid(), team.getmembers(), team.getgameid(), team.isclientdatasynced(), team.getjob(), team.getdigit(), team.getpoint()); realm.executetransaction(new realm.transaction() { @override public void execute(realm realm) { realm.copytorealmorupdate(rteam); } }); } { if(realm != null) { realm.close(); } } }
Comments
Post a Comment