2

我目前正在为我的应用程序使用领域,但是当我读取 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课程时,它不为空

4

1 回答 1

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();
于 2016-06-20T01:58:30.150 回答