错误的事务状态(没有活动事务,错误的事务类型或事务正在进行中)

时间:2016-06-19 21:21:38

标签: android database realm realm-migration

我目前正在为我的应用程序使用领域但是当我读到n写这个错误显示时,我检查了 beginTransaction() commitTransaction() < / strong>也正确使用但仍显示

错误:

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 类时,它不是null

1 个答案:

答案 0 :(得分:0)

所以,我解决了这个问题,当我更清楚地看到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();