更新Realm数据库的正确方法?

时间:2017-01-21 07:13:22

标签: java android database realm

这是我的Realm数据库的结构:

    public class ARDatabase extends RealmObject
    {
    @PrimaryKey
    private String uid;

    private String namex;
    private String desc;
    private boolean isVideo;
    private boolean isDeleted;
    private String urlImg;
    private String urlApp;
    private int updates;
    private boolean isDownloaded;
    private String location;

    public ARDatabase(){}

    public String getUid()
    {
        return uid;
    }

    public void setUid(String uid)
    {
        this.uid = uid;
    }

    public String getNamex()
    {
        return namex;
    }

    public void setNamex(String namex)
    {
        this.namex = namex;
    }

    public String getDesc()
    {
        return desc;
    }

    public void setDesc(String desc)
    {
        this.desc = desc;
    }

    public boolean getIsVideo()
    {
        return isVideo;
    }

    public void setIsVideo(boolean isVideo)
    {
        this.isVideo = isVideo;
    }

    public boolean getIsDeleted()
    {
        return isDeleted;
    }

    public void setIsDeleted(boolean isDeleted)
    {
        this.isDeleted = isDeleted;
    }

    public String getUrlImg()
    {
        return urlImg;
    }

    public void setUrlImg(String urlImg)
    {
        this.urlImg = urlImg;
    }

    public String getUrlApp()
    {
        return urlApp;
    }

    public void setUrlApp(String urlApp)
    {
        this.urlApp = urlApp;
    }

    public int getUpdates()
    {
        return updates;
    }

    public void setUpdates(int updates)
    {
        this.updates = updates;
    }

    public boolean getIsDownloaded()
    {
        return isDownloaded;
    }

    public void setIsDownloaded(boolean isDownloaded)
    {
        this.isDownloaded = isDownloaded;
    }

    public String getLocation()
    {
        return location;
    }

    public void setLocation(String location)
    {
        this.location = location;
    }
    }

我可以成功地将对象添加到数据库中 当我需要更新对象时会出现问题。

这就是我的尝试:

private void downloadUpdateDatabase(String uid,String location_address) throws RealmException
{

    mRealm.beginTransaction();
    ARDatabase db = new ARDatabase();
    db.setUid(uid);
    db.setIsDownloaded(true);
    db.setLocation(location_address);
    mRealm.copyToRealmOrUpdate(db);
    mRealm.commitTransaction();
    Log.e("TAG","DOWNLOAD UPDATE COMPLETED");

}

这里的问题是我调用此方法时。提到的字段会更新,但此方法中未提及的字段将变为null或零 当然,我可以通过调用它们的setter来设置所有字段的值,但是从我调用此方法的地方,我无法获得所有字段值。

所以,问题是:如何以现有字段不变为空的方式更新我的领域数据库?

P.S:

  

My Realm版本为:0.84.1,compile 'io.realm:realm-android:0.84.1'

2 个答案:

答案 0 :(得分:3)

  

提到的字段会更新,但此方法中未提及的字段将变为null或零

嗯,是的,此时所有字段都是默认值。

ARDatabase db = new ARDatabase();

您是否尝试过查询当前记录,然后更新字段,然后将该对象放回去?

换句话说,你有String uid,所以像

private void downloadUpdateDatabase(String uid,String location_address) throws RealmException
{
    mRealm.beginTransaction();
    ARDatabase db = mRealm.where(ARDatabase.class).equalTo("uid", uid).findFirst();
    db.setIsDownloaded(true);
    db.setLocation(location_address);
    mRealm.copyToRealmOrUpdate(db);
    mRealm.commitTransaction();
}

或者,probably better in async fashion

private void downloadUpdateDatabase(final String uid, final String location_address) throws RealmException
{
    mRealm.executeTransactionAsync(new Realm.Transaction() {
        @Override
        public void execute(Realm realm) {
            ARDatabase db = realm.where(ARDatabase.class).equalTo("uid", uid).findFirst();
            db.setIsDownloaded(true);
            db.setLocation(location_address);
        }
    }, new Realm.Transaction.OnSuccess() {
        @Override
        public void onSuccess() {
            // Transaction was a success.
        }
    }, new Realm.Transaction.OnError() {
        @Override
        public void onError(Throwable error) {
            // Transaction failed and was automatically canceled.
        }
    });
}

答案 1 :(得分:2)

而不是

mRealm.beginTransaction();
ARDatabase db = new ARDatabase();
db.setUid(uid);
db.setIsDownloaded(true);
db.setLocation(location_address);
mRealm.copyToRealmOrUpdate(db);
mRealm.commitTransaction();
Log.e("TAG","DOWNLOAD UPDATE COMPLETED");

应该有

mRealm.executeTransaction(new Realm.Transaction() {
    @Override
    public void execute(Realm realm) {
        ARDatabase db = realm.where(ARDatabase.class).equalTo("uid", uid).findFirst();
        if(db == null) {
            db = realm.createObject(ARDatabase.class, uid);
        }
        db.setIsDownloaded(true);
        db.setLocation(location_address);
    }
});
Log.e("TAG","DOWNLOAD UPDATE COMPLETED");