在添加对象后,Realm对象不会更新

时间:2015-09-09 08:14:29

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

有人可以向我解释一下吗? 这是我的代码,之后,我将显示日志。事实是我有一个对象列表。我记录它的大小: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

1 个答案:

答案 0 :(得分:2)

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

                    // 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);
}

我认为你需要“深度复制”你的arraylist:

这是错误的:

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.
    }
}
相关问题