【问题标题】:Realm object does not update, after adding objects in it领域对象不更新,在其中添加对象后
【发布时间】:2015-09-09 08:14:29
【问题描述】:

谁能给我解释一下? 这是我的代码,之后,我将显示日志。事实是我有一个对象列表。我记录它的大小:1。我清除它,变成 0。我在其中添加对象,它仍然是 0。

这是我的代码:

 Log.i("", "update step pitstop 1 BEFORE TEST FOR ACTIVE -> ACTIVE" + index);
                    RealmList<TripStep> tripsteps = psTrip.getTripSteps();
                    Log.i("", "update step pitstop 1 BEFORE TEST FOR ACTIVE -> ACTIVE2:" + psTrip.getTripSteps().size());
                    realmActive.beginTransaction();
                    TripStep tripStep = realmActive.copyToRealmOrUpdate(updateStepResponse.getStep());
                    realmActive.commitTransaction();
                    Log.i("", "update step pitstop 1 BEFORE TEST FOR ACTIVE -> ACTIVE3 + index is:" + index);
                    realmActive.beginTransaction();
                    tripsteps.set(index, tripStep);
                    realmActive.commitTransaction();
                    Log.i("", "update step pitstop 1 BEFORE TEST FOR ACTIVE -> ACTIVE4");
                    if (isRoaming) {
                        if ((tripsteps.size() - 2 == index) && !Utils.hasStarted(PSNewJourneyActivity.this)) {
                            realmActive.beginTransaction();
                            tripsteps.get(tripsteps.size() - 1).setTravelMode(updateStepResponse.getStep().getTravelMode());
                            realmActive.commitTransaction();
                        }
                    }
                    Log.i("", "update step pitstop 1 BEFORE TEST FOR ACTIVE -> ACTIVE5 tripsteps not in pstrip:" + tripSteps.size());
                    Log.i("", "update step pitstop 1 BEFORE TEST FOR ACTIVE -> ACTIVE5 tripsteps not in pstrip before:" + psTrip.getTripSteps().size());
                    realmActive.beginTransaction();
                    psTrip.getTripSteps().clear();
                    realmActive.commitTransaction();
                    Log.i("", "update step pitstop 1 BEFORE TEST FOR ACTIVE -> ACTIVE5 tripsteps not in pstrip AFTER CLEAR:" + psTrip.getTripSteps().size());
                    realmActive.beginTransaction();
                    for (TripStep tripStep1 : tripsteps){
                        psTrip.getTripSteps().add(tripStep1);
                    }
                    realmActive.commitTransaction();
                    realmActive.beginTransaction();
                    realmActive.copyToRealmOrUpdate(psTrip);
                    realmActive.commitTransaction();
                    Log.i("", "update step pitstop 1 BEFORE TEST FOR ACTIVE -> ACTIVE6 tripstep size:" + psTrip.getTripSteps().size());
                    Handler han = new Handler();
                    han.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            continueInit(true, true);
                            ProgressForMap(true);
                        }
                    }, mapDelay);

这是我的日志:

update step pitstop 1 BEFORE TEST FOR ACTIVE -> ACTIVE0
09-09 10:12:11.219  32740-32740/nl.hgrams.passenger I/﹕ update step pitstop 1 BEFORE TEST FOR ACTIVE -> ACTIVE2:1
09-09 10:12:11.251  32740-32740/nl.hgrams.passenger I/﹕ update step pitstop 1 BEFORE TEST FOR ACTIVE -> ACTIVE3 + index is:0
09-09 10:12:11.257  32740-32740/nl.hgrams.passenger I/﹕ update step pitstop 1 BEFORE TEST FOR ACTIVE -> ACTIVE4
09-09 10:12:11.257  32740-32740/nl.hgrams.passenger I/﹕ update step pitstop 1 BEFORE TEST FOR ACTIVE -> ACTIVE5 tripsteps not in pstrip:1
09-09 10:12:11.257  32740-32740/nl.hgrams.passenger I/﹕ update step pitstop 1 BEFORE TEST FOR ACTIVE -> ACTIVE5 tripsteps not in pstrip before:1
09-09 10:12:11.264  32740-32740/nl.hgrams.passenger I/﹕ update step pitstop 1 BEFORE TEST FOR ACTIVE -> ACTIVE5 tripsteps not in pstrip AFTER CLEAR:0
09-09 10:12:11.277  32740-32740/nl.hgrams.passenger I/﹕ update step pitstop 1 BEFORE TEST FOR ACTIVE -> ACTIVE6 tripstep size:0

【问题讨论】:

    标签: android database sql-update realm realm-list


    【解决方案1】:

    这有点令人费解。我要说的一件事是:

                        // this part makes sense
                        realmActive.beginTransaction(); 
                        for (TripStep tripStep1 : tripsteps){
                            psTrip.getTripSteps().add(tripStep1);
                        }
                        realmActive.commitTransaction();
    
                        // why are you then doing this?
                        //realmActive.beginTransaction();
                        //realmActive.copyToRealmOrUpdate(psTrip);
                        //realmActive.commitTransaction();
    

    据我所知,仅第一部分就足够了,第二部分什么都不做。

    更新:

    刚刚注意到您的变量 tripsteps 被分配在这里:

    RealmList<TripStep> tripsteps = psTrip.getTripSteps();
    

    然后你再这样做:

    psTrip.getTripSteps().clear();
    

    tripstepspsTrip.getTripSteps() 指向同一个对象,因此当您调用 psTrip.getTripSteps().clear(); 时,您也会清除 tripsteps

    所以此时tripsteps 中将没有任何内容:

    for (TripStep tripStep1 : tripsteps){ // tripsteps.size() == 0
        psTrip.getTripSteps().add(tripStep1);
    }
    

    我认为你需要“深拷贝”你的数组列表:

    这是错误的:

    RealmList<TripStep> tripsteps = psTrip.getTripSteps();
    

    改为这样做:

    RealmList<TripStep> tripsteps = cloneList(psTrip.getTripSteps());
    
    public List<TripStep> cloneList(RealmList<TripStep> list) {
        List<TripStep> clone = new ArrayList<TripStep>(list.size());
        for(TripStep item : list){
            clone.add(new TripStep(item));
        }
    
        return clone;
    }
    

    您需要在 TripStep 类中创建一个可以从另一个实例复制的构造函数:

    class TripStep
    {
        public TripStep()
        { ... } // Regular constructor
    
        public TripStep(TripStep cloneFrom) {
            // Copy all the fields of TripStep.
        }
    }
    

    【讨论】:

    • 我忘记拿出来了。这是一个绝望的举动,试图看看它是否有任何帮助。但这不起作用,它将为 0。
    • 它适用于添加的第二个。将设置为已解决,非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-17
    • 1970-01-01
    相关资源
    最近更新 更多