错误的交易状态(没有活动的交易、错误的交易类型或交易已经在进行中)

Wrong transactional state (no active transaction, wrong type of transaction, or transaction already in progress)

我目前正在为我的应用程序使用领域,但是当我读 n 写时显示此错误,我已经检查了 beginTransaction()commitTransaction() 也被正确使用但仍然显示

错误:

io.realm.exceptions.RealmError: Unrecoverable error. Wrong transactional state (no active transaction, wrong type of transaction, or transaction already in progress) in io_realm_internal_SharedGroup.cpp line 157
                                                                   at io.realm.internal.SharedGroup.nativeAdvanceRead(Native Method)
                                                                   at io.realm.internal.SharedGroup.advanceRead(SharedGroup.java:83)
                                                                   at io.realm.internal.ImplicitTransaction.advanceRead(ImplicitTransaction.java:35)
                                                                   at io.realm.internal.SharedGroupManager.advanceRead(SharedGroupManager.java:76)
                                                                   at io.realm.HandlerController.realmChanged(HandlerController.java:384)
                                                                   at io.realm.HandlerController.handleMessage(HandlerController.java:116)
                                                                   at android.os.Handler.dispatchMessage(Handler.java:98)
                                                                   at android.os.Looper.loop(Looper.java:136)
                                                                   at android.app.ActivityThread.main(ActivityThread.java:5052)
                                                                   at java.lang.reflect.Method.invokeNative(Native Method)
                                                                   at java.lang.reflect.Method.invoke(Method.java:515)
                                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
                                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609)
                                                                   at dalvik.system.NativeStart.main(Native Method)

代码片段是:

 HashMap<String,String> msg = (HashMap)dataSnapshot.getValue();
        Message message = new Message();
        message.setSender(msg.get(COLUMN_SENDER));
        message.setText(msg.get(COLUMN_TEXT));
        message.setTime(msg.get(COLUMN_TIME));
        message.setStatus(msg.get(COLUMN_STATUS));

        try {
            Realm realm = Realm.getInstance(realmConfig);
            Realm realm2 = Realm.getInstance(realmConfig);

            message.setDate(sDateFormat.parse(SnapshotKey));
          //  Hawk.put(SnapshotKey, message);
            RealmResults<MessageRealm> result = realm.where(MessageRealm.class)
                    .equalTo("key", SnapshotKey)
                    .findAll();
            if (result.size()==0){
                realm.beginTransaction();
                MessageRealm messageRealm=realm.createObject(MessageRealm.class);
                messageRealm.setSender(msg.get(COLUMN_SENDER));
                messageRealm.setText(msg.get(COLUMN_TEXT));
                messageRealm.setStatus(msg.get(COLUMN_STATUS));
                messageRealm.setTime(msg.get(COLUMN_TIME));
                messageRealm.setKey(SnapshotKey);
                messageRealm.setChatWith(ReciOtherEmail);
                realm.commitTransaction();

                String userEmailKeyY=ReciOtherEmail;
                String keyEmail=userEmailKeyY;
                Log.e("SahajLOG5", "KeyEmail  " + userEmailKeyY);
                Log.e("SahajLOG", "REALM ADDED TRANSACTION  INITIATED   ");
                RealmResults<ToTal_ProfileModelRealmClass> resultForProfileInfo = realm2.where(ToTal_ProfileModelRealmClass.class)
                        .equalTo("userEmailKey", keyEmail)
                        .findAll();
                Log.e("SahajLOGL", "REALM REsult <--CorrectionLegacy--> " + resultForProfileInfo);
                if(resultForProfileInfo.size()!=0){
   -->                 realm2.beginTransaction();
   -->                 currentChatRealmModelClass obj=realm.createObject(currentChatRealmModelClass.class);
   -->                 obj.setUserEmailKey(keyEmail);
   -->                 obj.setMessage(msg.get(COLUMN_TEXT));
   -->   obj.setHashname(resultForProfileInfo.get(0).getHashname());
   -->  obj.setUserNAME(resultForProfileInfo.get(0).getUserNAME());
   -->  obj.setPicOfllineName(resultForProfileInfo.get(0).getPicOfflineName());
   -->                 obj.setPicurl(resultForProfileInfo.get(0).getPicurl());
   -->                     realm2.commitTransaction();
                }

            }



        }catch (Exception e){
            Log.d(TAG, "Couldn't parse data in MessageDataSource Class   " + e);
        }
        if(callbacks != null){
            callbacks.onMessageAdded(message);
        }

我已经用 --> 标记了实际发生的地方。

n 当我记录 resultForProfileInfo class 时,它不是 null

所以,我解决了这个问题,当我更清楚地查看 logcat 时,还有一个错误指出我已经有了主键,它又被保存了..所以,我做了一个新的realmClass 的对象,然后在其中更新它。

 -->     currentChatRealmModelClass obj=new currentChatRealmModelClass();//realm.createObject(currentChatRealmModelClass.class);
                    obj.setUserEmailKey(keyEmail);
                    obj.setMessage(msg.get(COLUMN_TEXT));
                    obj.setHashname(resultForProfileInfo.get(0).getHashname());
                    obj.setUserNAME(resultForProfileInfo.get(0).getUserNAME());
                    obj.setPicOfllineName(resultForProfileInfo.get(0).getPicOfflineName());
                    obj.setPicurl(resultForProfileInfo.get(0).getPicurl());
       -->          realm2.beginTransaction();
       -->          realm.copyToRealmOrUpdate(obj);
       -->          realm2.commitTransaction();